Check for Palindromes why my code don't work if it should? [Spoiler]

Check for Palindromes why my code don't work if it should? [Spoiler]
0

#1

Hi! after browsing forum i found what i have to change, and Icame with solution:
function palindrome(str) {
str = str.replace(/\W+/g, “”).replace(/_/g, “”).toLowerCase();
var str1 = str.split("").reverse().join("");
if (str == str1){
return true;
}
return false;
}

BUT I don’t know why if i make: str = str.replace(/[0-9]/g,"").replace(/\W+/g, “”).replace(/_/g, “”).toLowerCase(); code dosen’t work, becouse with “1 eye for of 1 eye.” I’m geting true, not false. From what i understood i first it replace all numbers, then characters and then _ and after it make it all lower case. Why it isn’t working, if only it’s just longer?


#2

You shouldn’t be replacing 0-9, only non-alphanumeric characters.


#3

OMG :smiley: I’m so stupid :smiley: thanks! I should be more attentive :slight_smile:


#4

No worries, we all get code-blindness from time to time :slight_smile: