A Little Clarification - "Check for Palindromes" Exercise

Ok, so I am totally new to the world of coding, etc and I don’t really know where else to ask this quiestion. I don’t know anyone else who codes, so I was wondering if you guys could clarify something for me.

After struggling with the Check for Palindromes exercise for days, I finally gave in and used the Get A Hint to try figuring it out. After realizing that I was automatically attempting to go the Intermediate route (as opposed to even considering the Basic route), I found myself stuck on a possibly wholely unrelated question.

Here’s the code I finally ended up entering:

function palindrome(str) { // Good luck! return str.replace(/[\W_]/g, '').toLowerCase() === str.replace(/[\W_]/g, '').toLowerCase().split('').reverse().join(''); }

palindrome(".123 Eye 321.");


Where the heck does it get the information to return True or False if it’s not in the code? I’m not understanding that part. Can anyone explain? Thanks a ton, and sorry in advance if this has already been asked before.

see that === ?
that’s comparison operator that only return true if the value and type of data is equal

1 Like

The solution to the problem would look something like this:

  1. Find a word or sentence you want to check.
  2. If its a sentence remove all the characters that are not part of the alphabet.
  3. If there are any capital letters transform them to lower case and write the result down.
  4. Take the result and write it backwards.
  5. If the two are identical then it is a palindrome.

Those five steps can be done on paper first. The example above may be a bit hard to figure out for a person who is just getting into coding. So lets brake it up into steps.

function palindrome(str) {
   // 1. Remove all symbols
   var noSymbolsString = str.replace(/[\W_]/g, '');

   // 2. Convert to lowercase
   var lowerCaseString = noSymbolsString.toLowerCase();

   // 3. Write the string backwards
   var reverseString = lowerCaseString.split('').reverse().join('');

   // 4. Compare and return the result
   return lowerCaseString === reverseString;
}

1. Remove all symbols

Here you use a regex expression to replace all the symbols that are getting in the way. In regex /…/g means that the specified rule should be matched multiple times. [\W_] look for non word characters. So str.replace(/[\W_]/g, ‘’) looks for all the non word characters in the string and replaces them with an empty character (in other words removes them).

2. Convert to lowercase

Pure and simple the toLowerCase function of String will replace all capital letters with lower case once since javascript is case sensitive.

3. Write the string backwards

The go to way to reverse a string in javascript is to convert it into an array of characters reverse the order of the elements in the array and then convert the array into a string.

var str = "one";

str = str.split('');
// ==> ["o", "n", "e"];

str = str.reverse();
// ==> ["e", "n", "o"];

str = str.join('');
// ==> "eno";

4. Compare and return the result

A Boolean comparison of the original transformed and reverse transformed string that returns either true or false. That is where the code would figure out what to return.

In the original version of the function all of this is compacted into a single line so it might be a bit hard to see. Hope this made it a bit clear :slight_smile: .

1 Like

Thank you so much for the explanation! It helped a LOT!!