Here is the soluction I came up with:
function palindrome(str) {
const clean = str.toLowerCase().replace(/[^a-z0-9]/g, '');
const strReverse = clean.split('').reverse().join('');
return clean == strReverse;
}
console.log(palindrome("0_0 (: /-\ :) 0-0"));
2 Likes
hey All, check out my solution here:
function palindrome(str) {
let alphaNum = /[^A-Za-z0-9]/g; //regex
let stripStr = str.replace(alphaNum, ""); //remove non-alphanumeric
let strToUpper = stripStr.toUpperCase();//string to upper case
let arrayStrToUpper = strToUpper.split(""); //string to array
//debugging the above string and array
console.log(strToUpper);
console.log(arrayStrToUpper);
let revStrArray = arrayStrToUpper.reverse();//reverse array
let newStr = revStrArray.join("");//array to string
//debugging the above string and array
console.log(newStr);
console.log(revStrArray);
//conditional test for palindrome
if (newStr == strToUpper) {
return true;
}
else {
return false;
}
}
palindrome("eye");
palindrome("race car");
palindrome("_eye");
palindrome("0_0 (: /-\ :) 0-0");
console.log(palindrome("not a palindrome"));`Preformatted text`
1 Like
So it was supposed to be an application of the learnings from the first 3 sections. But it was hard for me not to use a join array method in the if statement
// 🎉 Completed on 3-23-22 D22_W4_R1 of #100DaysOfCode
function palindrome(str) {
let upStr = str.toUpperCase(); // uppercase
let regex = /[A-Z0-9]/gi; // capital letters and numbers only
let regArr = upStr.match(regex); // original order in array form
let length = regArr.length - 1; // total elements - zero based
let newArr = []; // storage for the reversed order
for(let j = length; j >= 0; j--){ // j starts as the last element
newArr.push(regArr[j]); // j pushed into the array (reverse order)
if(newArr.join() === regArr.join()){ // join to make 'em string
return true;
}
}
return false;
}
console.log(palindrome("almostomla"));
1 Like