# Check for Palindromes- I dunno whats wrong with my code

Check for Palindromes- I dunno whats wrong with my code
0

#1

Tell us what’s happening:

function palindrome(str) {
// Good luck!

str = str.replace(/[&\/\\#,+()\$~%.'":*?<>{} ]/g,'');
str = str.toLowerCase;
var i;

var newStr = "";

for(i = str.length-1; i >= 0; i--) {
newStr = newStr + x[i];

}

if(newStr == str)
return true;
else
return false;

}

palindrome("eye");

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36.

#2

3 steps

first is str.replace, the chosen characters dont pass all the tests

str = str.replace(/[&\/\\#,+()\$~%.'":*?<>{} ]/g,'');

lets make it

str = str.replace(/[^A-Za-z0-9]/g,'');

it’s now replacing all non alphanumeric characters, notice the ^ symbol, which basically reverses the regular expression to find anything that isnt within A-Z a-z or 0-9

Second
you left off the parenthesis at the end of

str = str.toLowerCase;  // should be str=str.toLowerCase();

Third

for(i = str.length-1; i >= 0; i--) {
newStr = newStr + x[i]; // should be str[i]

}

in your for loop you reference x[i]; but you never declared a variable x.
you need it to be str[i]

Good luck!

#3

Thanks I made all the changes and it worked like a charm. So, silly of me for not declaring x.

#4

Also way using a regular expression to replace all non-alphanumeric characters is:

str = str.replace(/\W|_/g,'');

The \W metacharacter is used to find a non-word character. A word character is a character from a-z, A-Z, 0-9, including the _ (underscore) character.

The | stands for OR.

The _ is to find underscore characters. This has to be in here, because the \W does not consider the underscore to be a non-word character.

Putting it all together the regular expression replaces all non-word characters and underscores with a blank string.

#5

Thanks for your explanation I will come up with more query