Check for Palindromes Fails Only for Periods

Check for Palindromes Fails Only for Periods
0

#1

Tell us what’s happening:
Got most of the way through the Palindrome challenge but my code fails on the two with a period in them. I’m not sure why this is since I believe my regex should remove all non alphanumerics.

Your code so far

function palindrome(str) {
  // Good luck!
  var forwardStr = str.replace(/[\W_]/g, ''); // Removes all non-alphanumeric characters
  forwardStr.toLowerCase(); // Changes all characters to lowercase
  
  var tempArr = forwardStr.split(''); // Splits the modified string into an array of individual characters
  tempArr.reverse(); // Reverses the array
  var backwardStr = tempArr.join(''); // Joins the reversed array back into a whole string
  
  if (forwardStr === backwardStr) {
    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/61.0.3163.100 Safari/537.36.

Link to the challenge:


#2

Use this regExp:

/[^A-Za-z0-9]/gi

it removes everything but letters and digits


#3

This still causes the code to fail for the ones with a period in them. :confused: Not sure how it’s possible that those two fail while everything else passes.


#4

Your regex is fine. The problem is the following line:

forwardStr.toLowerCase();

You think it is making forwardStr all lowercase, but it is not. It does not, because it returns a lowercase version, but does not alter the original string itself. You need to do the following:

forwardStr = forwardStr.toLowerCase();

#5

Thank you! I feel really foolish for not realizing this sooner!