Awesome job, friend. Compact!!!
If you care, you could just write return (justAlpha === reversedStr). Equality operators (in this case, the strict ===) evaluate to a Boolean result (true or false). so, justAlpha === reversedStr would return true or false.
Here’s a helpful link - Strict equality (===) - JavaScript | MDN
let checkPal = replaceVal.split(‘’).reverse().join(‘’);
//strictly compare forward and reverse strings to identify palindromes
return (checkPal === replaceVal);
}
console.log(palindrome(“eye”));//console log as many potential palindrome string types as possible to see what output is printed to the console
My solution may be a bit cumbersome. a’s first element is compared to its last element, and if it’s the same, b will increase by one. And its second element is compared to its last but one element, and if it’s the same, b will increase by one again, and so one. And then at the end, the first element will be compared to its element again. If b equal a.length (whether a.length is odd or even number), the given string is a palindrome and return true.
And forgive my bad English expression please.
function palindrome(str) {
let lstr=str.toLowerCase();
let strregex = /[a-z0-9]/gi;
let a = lstr.match(strregex);
let b=0;
for (let i=0;i<a.length;i++){
if (a[i]===a[a.length-i-1]){
b+=1;
}
}
if (b===a.length){
return true;
}else{
return false;
}
}
function palindrome(str) {
//remove all non-alphanumeric characters
let cleanStr = str.replace(/\W|_/g, '');
// covert to lowercase;
cleanStr = cleanStr.toLowerCase();
// reverse string
let reversedStr = cleanStr.split('').reverse().join('');
// Compare cleanStr and reversedStr
for (let i = 0; i < cleanStr.length; i++) {
// if a letter does not match return false
if (cleanStr[i] != reversedStr[i]) {
return false;
}
}
// cleanStr and reversedStr match, return true
return true;
}
//replaces all non alphanumeric and underscores
function removeNonAlpha(str) {
return str.replace(/[\W_]/g,"");
};
//split the string into an array, reverse it and then rejoin it back into a string
function flipString(str) {
return str
.split("")
.reverse()
.join("");
};
function palindrome(str) {
//remove non-alphanumeric characters using function above
const cleanString = removeNonAlpha(str);
//make clean string uppercase
const uppercaseString = cleanString.toUpperCase();
//reverse uppercase string using flipString function
const reverseString = flipString(uppercaseString);
//compare reversed and uppercase string - if they are the same then return true
if (uppercaseString === reverseString) {
return true;
} else {
return false;
}
}
palindrome("eye");
// FUNCTION TO remove all non-alphanumeric characters
function cleanUp(str) {
return str.replace(/[\W_]/g, "");
}
// FUNCTION TO Turn all to a smaller case
function makeSmaller(str) {
return str.toLowerCase();
}
// FUNCTION TO Reverse to smaller case
function revSmaller(str) {
//Create an empty string
let result = "";
//Create a loop that reverses it
for (let i = str.length-1; i>=0; i--) {
result += str[i];
}
// return result str
return result;
}
function palindrome(str) {
// Firstly remove all non-alphanumeric characters
const cleanedUpStr = cleanUp(str);
// Turn all to a smaller case
const makedSmallerStr = makeSmaller(cleanedUpStr);
// Reverse smaller case
const revdSmallerStr = revSmaller(makedSmallerStr);
console.log(revdSmallerStr);
// compare if same then return true
return revdSmallerStr === makedSmallerStr;
}
console.log(palindrome("DOD"));
function palindrome(str) {
//initalizes variables
let isPalindrome = true;
const cleanReg = /[a-z0-9]/g;
let countTo;
const cleanArr = str.toLowerCase().match(cleanReg); //sends string to lowercase turns it into an array, cleaning it of non-alphanumerics
const arrLen = cleanArr.length-1; //captures length of array
//determines how far the below for loop should run, depending on whether arrLen is even or odd
if (arrLen % 2 != 0){
countTo = arrLen/2;
} else {
countTo = (arrLen-1)/2;
}
//compares each entry in the array with its mirror and sets isPalindrome to false if ever it does not match
for (let i = 0; i < countTo; i++) {
if (cleanArr[i] != cleanArr[arrLen-i]){
isPalindrome = false;
}
}
return isPalindrome;
}