Algorithm 101: 3 Ways to Check if a Sentence is a Palindrome
Tags: algorithms, javascript, palindrome, beginners
Published: 2020-03-13 14:09:04

In this episode of Algorithm 101, I will be fulfilling my promise to you. When I made the article on checking for [word palindrome](https://dev.to/ebereplenty/algorithm-101-6-ways-to-check-if-a-word-is-a-palindrome-40a3), I promised you an article for sentence palindrome.

```javascript

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

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

```
### 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()

```javascript
      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;
      }
```

* regular expression, toLowerCase(), split(), for...of...loop, match(), reverse(), join(), push()

```javascript
      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;
      }
```

* toUpperCase(), toLowerCase(), split(), forEach...loop, reduce(), join(), push()

```javascript
      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;
      }
```

### 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](https://dev.to/ebereplenty/algorithm-101-3-ways-to-check-if-two-sentences-are-anagrams-55jc)

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

**[Twitter](https://twitter.com/eberetwit) | [LinkedIn](https://www.linkedin.com/in/samson-ebere-njoku-profile/) | [Github](https://github.com/EBEREGIT)**

Thank You For Your Time.