palindrome checker javascript

Palindrome checker

JavaScript

Exercise

back

Palindrome app

If you haven't checked my 'Palindrome checker' application, you may do it right now.

In this article, I am going to explain optional 2 concepts, that you may use to solve this problem - to check if a string is a Palindrome.

What is a Palindrome?

A palindrome is a word, number, phrase, or another sequence of characters, that reads the same backward as forward. The examples of palindromes are madam, 121, mom, or level.

JavaScript concepts

I am sure that there are a lot of JavaScript concepts that could be used to solve this problem. However, I picked the ones that seemed to match the best – at least from my point of view.

In these examples, I do not access any DOM elements. These are just functions and the results will be logged to the console.

If you're interested in checking my application source code, feel free to dive deeper into my Github repository.

1st concept - split, reverse and join methods

The function takes one parameter – text. I am converting the text toLowerCase() and to a String() – just in case someone decides to check if numbers are a palindrome or types a string with lowercase and uppercase. In the result any input value (including numbers) will be converted to lowercase and to a string.

As the reverse() method doesn't work on strings, I am converting a string to array. Then I reverse the entire array and join() the values. I use the ternary operator to log the result to the console.

              
const checkPalindrome = (text) => {
    const inputToLowerCase = String(text.toLowerCase());
    const newString = inputToLowerCase.split('').reverse().join('');

    inputToLowerCase === newString
    ? console.log(`Input: ${inputToLowerCase}\nOutput: ${newString}\n${inputToLowerCase} is a palindrome !`)
    : console.log(`Input: ${inputToLowerCase}\nOutput: ${newString}\n${inputToLowerCase} is NOT a palindrome !`);
    };
              
          
              
checkPalindrome('Mom');
//OUTPUT
// Input: mom
// Output: mom
// mom is a palindrome !
              
          

2nd concept – for loop

This concept may look a bit cleaner to you. I am looping through a string. The iteration starts from the end to the beginning of a string and will keep looping until the condition is true. In this case if i(index) >= 0.

Each iteration's value (single letter) is saved to the newString variable.

With the help of the ternary operator, the result will be logged to the console.

              
const checkPalindrome = (text) => {
    const inputToLowerCase = String(text.toLowerCase());
    let newString = '';

    for (let i = inputToLowerCase.length - 1; i >= 0; i--) {
    newString = newString + inputToLowerCase.at(i);
    }

    inputToLowerCase === newString
    ? console.log(`Input: ${inputToLowerCase}\nOutput: ${newString}\n${inputToLowerCase} is a palindrome !`)
    : console.log(`Input: ${inputToLowerCase}\nOutput: ${newString}\n${inputToLowerCase} is NOT a palindrome !`);
    };
              
          
              
checkPalindrome('MOM');
//OUTPUT
// Input: mom
// Output: mom
// mom is a palindrome !