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