Palindromes - half done is well begun

I know the subject is really the other way around but describes my situation.

So I really thought this one out. I really waited until I felt I had made my best effort before asking for help. And I have looked at another solution, but it was not helpful to me even though it was a mostly working solution. I’d like to learn something here so thanks in advance for your patience. My Code:

function palindrome(str) {
  // Good luck!
  //  The following is the/a correct regex to strip non-alphanumeric chars from an input string:


  var palintest = str;
  palintest.replace(/\W/g, '');
  palintest.toLowerCase();
  var strReverse = str.split().reverse().join();
  
  if (str === strReverse){
    return true;
  } else {
    return false;
  }
  
  
}



palindrome("eye");

Gee, can you tell I’m a novice? So my reasoning was as follows.

First I needed a place to store the value of str so I created the variable palintest
Next the problem was to remove all non alphanumeric characters from str. I found this statement in a Google Search:
palintest.replace(/\W/g, ''); Now, one thing I’d like to know is the significance or translation of /\W/g, All I really understand is that it sort of works.
Next I had to make all the characters lowercase and I followed the link in the exercise to get this;
palintest.toLowerCase();

Then I harkened back to a previous lesson on reversing a string where I got this: var strReverse = str.split().reverse().join();

Then I reasoned that if str equaled its reverse then that’s a palindrome. And that part seems to work pretty well. At least for returning true. The only string that should have returned true and didn’t was: palindrome(“My age is 0, 0 si ega ym.”) and that has spaces in it. So a clue there.

not a palindrome returned false as it should have but ‘nope’ did not.

So I need some enlightenment. Thanks in advance.

luciano991

Look carefully at what variables you’re manipulating in the first section, and if they’re consistent.

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.


  1. You’ve run into a common devil-in-the-details issue with \W. \W matches any “non-word character” The tricky part here is what is a “non-word character” . https://regex101.com/ is a great resource not just for testing regular expressions, but for looking up tokens

  2. You’ve fallen victim to one of the classic blunders of strings. Remember that strings are immutable.

  3. See @ahong08’s note above.

This is how I explained it to my students

I’d also add another hint, related to what @ahong08 said: you only need two variables in your method.

You’re very close to the solution. Keep hacking away!