DEV Community

NJOKU SAMSON EBERE
NJOKU SAMSON EBERE

Posted on • Edited on

Algorithm 101: 3 Ways to Check if a Sentence is a Palindrome

In this episode of Algorithm 101, I will be fulfilling my promise to you. When I made the article on checking for word palindrome, I promised you an article for sentence palindrome.


sentencePalindrome("Was it a car or a cat I saw?"); // true

sentencePalindrome("Njoku, Samson Ebere. Plenty!"); // false

Enter fullscreen mode Exit fullscreen mode

Prerequisite

This article assumes that you have basic understanding of javascript's string and array methods.

Let's Check if a Sentence is a Palindrome using:

  • toLowerCase(), split(), map(), includes(), reverse(), join()
      function sentencePalindrome(sentence) {
        let newSentence = sentence.toLowerCase().split("");
        let newSentenceArray = [];
        let alphabet = "abcdefghijklmnopqrstuvwxyz";
        let numeric = "0123456789";
        let alphanumeric = [...alphabet, ...numeric];

        newSentenceArray = newSentence.map(char => {
          if (alphanumeric.includes(char)) {
            return char;
          }
        });

        let sanitizedSentence = newSentenceArray.join("");

        // reverse sentence
        let finalSentence = [...sanitizedSentence].reverse().join("");

        return finalSentence === sanitizedSentence;
      }
Enter fullscreen mode Exit fullscreen mode
  • regular expression, toLowerCase(), split(), for...of...loop, match(), reverse(), join(), push()
      function sentencePalindrome(sentence) {
        let newSentence = sentence.toLowerCase().split("");
        let regEx = /\w/gi;
        let newSentenceArray = [];

        for (char of newSentence) {
          if (char.match(regEx)) {
            newSentenceArray.push(char);
          }
        }

        let sanitizedSentence = newSentenceArray.join("");

        // reverse sentence
        let finalSentence = [...sanitizedSentence].reverse().join("");

        return finalSentence === sanitizedSentence;
      }
Enter fullscreen mode Exit fullscreen mode
  • toUpperCase(), toLowerCase(), split(), forEach...loop, reduce(), join(), push()
      function sentencePalindrome(sentence) {
        let newSentence = sentence.toLowerCase().split("");
        let newSentenceArray = [];

        function isLetter(char) {
          return char.toLowerCase() != char.toUpperCase();
        }

        function isDigit(char) {
          return char >= 0 && char <= 9;
        }

        newSentence.forEach(char => {
          if (isDigit(char) || isLetter(char)) {
            newSentenceArray.push(char);
          }
        });

        let sanitizedSentence = newSentenceArray
          .join("")
          .split(" ")
          .reduce((acc, char) => acc + char);

        // reverse sentence
        let finalSentence = [...sanitizedSentence].reduce(
          (acc, char) => char + acc
        );

        return finalSentence === sanitizedSentence;
      }
Enter fullscreen mode Exit fullscreen mode

Conclusion

There are many ways to solve problems programmatically. You are only limited by your imagination. You could try using a different looping construct other than what I have used. I will love to know other ways you solved yours in the comment section.

If you have questions, comments or suggestions, please drop them in the comment section.

Up Next: Algorithm 101: 3 Ways to Check if Two Sentences are Anagrams

You can also follow and message me on social media platforms.

Twitter | LinkedIn | Github

Thank You For Your Time.

Top comments (0)