The solution to Freecodecamp’s Palindrome Checker Project

The solution to Freecodecamp’s Palindrome Checker Project

Introduction

I checked into Freecodecamp to attempt some of the JavaScript projects in its JavaScript Algorithms and Data Structures Certification. The first project I engaged in was to create a simple palindrome checker.

This article walks the reader through the steps of completing this project. The article is aimed at developers who are currently going through Freecodecamp’s JavaScript Algorithms and Data Structures certification courses. Developers who are looking for a guide to complete the Palindrome checker project may also find this article helpful.

What is a Palindrome?

A palindrome is a word or sentence that is spelled the same way, forward and backward. Examples of a palindrome include “Eye”, “Madam”, and “never odd or even”. Non-alphanumeric characters including punctuations, spacing, and case have no effects on palindromes. Hence, ‘Eye.’ is the same thing as ‘eyE.’ even though the capitalizations and punctuation appear at different positions while spelling from either side.

The Palindrome Checker Challenge

Below is the project challenge as stated on freecodecamp

Palindrome Checker

Return true if the given string is a palindrome. Otherwise, return false.

A palindrome is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case, and spacing.

Note: You'll need to remove all non-alphanumeric characters (punctuation, spaces, and symbols) and turn everything into the same case (lower or upper case) in order to check for palindromes.

We'll pass strings with varying formats, such as racecar, RaceCar, and race CAR among others.

We'll also pass strings with special symbols, such as 2A33a2, 2A3 3a2, and 2_A33#A2.

Project Interpretation

This project tasks you to do the following:

  • Remove all characters that are not alphabets or numbers from the strings. Hence, if the string, ‘race CAR’ is passed, remove the whitespace between the words ‘race’ and ‘car’ to make it raceCAR.

  • Convert all letters to either upper case (all caps) or lower case. Hence, the string, ‘race CAR’ appears as either ‘racecar’ or ‘RACECAR’.

  • Check if the string is spelled exactly the same way when read from either side. Hence, when the string, ‘racecar’ is passed, the checker returns true. If the string, ‘Camera’ is passed, it returns false. This is because ‘Camera’ is not spelled the same way forward and backward; hence it is not a Palindrome.

What do you learn by completing the Palindrome Checker Project?

After completing this challenge, you will learn how to do the following:

  • convert letters to lowercase using the .toLowerCase() method

  • check only alphanumeric characters in the string using the .match() method

  • join the matched characters as one word using the .join() method

  • compare the reverse of the word with its original using the .reverse() method

How to Create a Palindrome Checker

The following steps will guide you to complete the Palindrome Checker project.

Declare a function named ‘palindrome’ to check for palindromes and pass ‘str’ as a parameter of the palindrome function.

function palindrome(str) {
}

Within the palindrome function, create a variable called alphaNumeric using the const keyword. Convert any word/sentence passed as arguments to all lowercase characters using the .toLowerCase() method. Also, make sure that you’re returning only alphanumeric characters. The .match() method achieves this. The results are assigned to the alphaNumeric variable.

const alphaNumeric = str.toLowerCase()
                      .match(/[a-z0-9]/g)

/[a-z0-9]/ in the .match() method is a regular expression (regex) which means that you should match all lowercase letters from a to z and all numerals from 0 to 9.

g is a global flag that tells the regular expression to match every possible occurrence.*

Check if the word is exactly the same when it is reversed.

return alphaNumeric.join('') ===
        alphaNumeric.reverse().join('');

*The .join(' ') method added to this line joins all the returned characters together, eliminating spacing and punctuation. *

If they are exactly the same, return true.

return true;

**That is it. **

Here is the entire code solution to the challenge:

function palindrome(str) {
  const alphaNumeric = str.toLowerCase()
                      .match(/[a-z0-9]/g)
   return alphaNumeric.join('') ===
        alphaNumeric.reverse().join('');
  return true;
}

Conclusion

I sincerely hope that my voice helps someone to understand this project better. Your feedback will be highly appreciated.