Palindromes - half done is well begun

Palindromes - half done is well begun
0.0 0

#1

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


#2

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


#3

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.


#4

Adding further to what @ahong08 said, why don’t you add console.log() after each of the following lines, to make sure you know what values are stored in the variables. You might be surprised by the actual values:

  palintest.replace(/\W/g, '');
  console.log(palintest);  // what should you see for palintest?
  palintest.toLowerCase();
  console.log(palintest);  // what should you see for palintest?
  var strReverse = str.split().reverse().join();
  console.log(strReverse );  // what should you see for strReverse?

#5

This is how I explained it to my students


#6

I do not think he wanted to see the full solution. He just wanted to be pointed in the right direction.


#7

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!