Whats wrong in this code please help...palindrom checker

Tell us what’s happening:

function palindrome(str) {

  

  str=str.toLowerCase().replace("/[\W_]/g", "");

  for(var i=0,len=str.length-1;i<len/2;i++){

if(str[i]!==str[len-i]){

  return false;

}

  }

  return true;

}

palindrome("eye");

Your code so far


function palindrome(str) {

str=str.toLowerCase().replace("/[\W_]/g", "");
for(var i=0,len=str.length-1;i<len/2;i++){
if(str[i]!==str[len-i]){
return false;
}

}

return true;
}



palindrome("eye");

Your browser information:

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

Challenge: Palindrome Checker

Link to the challenge:

The pattern for string.replace can either be a string or a Regex, therefore you should be careful how you pass the argument:

replace("/[\W_]/g", "");

As here you have passed a String as pattern, instead of a regex.

Regex example:

myString.replace(/[\D_]/g, "")

Hope it helps :sparkles:

Why can’t you use split, reverse, join on a string to test if it’s palindrome, your code will be reduced to 1-2 lines.

maybe, but it’s more performing with the loop. Also, golf code is rarely a good habit