Check for Palindromes- I dunno whats wrong with my code

Tell us what’s happening:

Your code so far

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 information:

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.

Link to the challenge:

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!

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

1 Like

Thanks for your explanation :slight_smile: I will come up with more query :smiley: