Stuck on Palindrome Challenge

Stuck on Palindrome Challenge
0.0 0

#1

Here’s my code-


function palindrome(str) {

  var newstr=str.replace(/[\W_]/gi,'');
  newstr.toLowerCase();
  var k=newstr.split("");
  k.reverse();
  var j=k.join("");
  return newstr===j;
}
palindrome("A man, a plan, a canal. Panama");

It doesn’t work for 2 test cases -
palindrome(“A man, a plan, a canal. Panama”) should return true.
palindrome(“My age is 0, 0 si ega ym.”) should return true.

While below code works fine-

function palindrome(str) {
  // Good luck!
 
  var newstr=str.toLowerCase().replace(/[\W_]/gi,'');
  var k=newstr.split("");
  k.reverse();
  var j=k.join("");
  return newstr===j;
}
palindrome("A man, a plan, a canal. Panama");

What’s the difference between these two?


#2

toLowerCase does not edit the string in place. You have to assign it to a variable.

function palindrome(str) {

  var newstr=str.replace(/[\W_]/gi,'');
  newstr = newstr.toLowerCase(); //store the result
  var k=newstr.split("");
  k.reverse();
  var j=k.join("");
  return newstr===j;
}