Algorithm 101: 6 Ways to Check if a Word is a Palindrome
Tags: algorithms, javascript, palindrome, word
Published: 2020-03-05 15:51:34

If you followed my [Algorithm 101 first episode](https://dev.to/ebereplenty/algorithm-101-7-ways-to-reverse-a-string-15cb), then this will be very easy. Here, the main objective is to reverse a given word and check if it still matches the given word.

```javascript
wordPalindrome("Racecar"); // true

wordPalindrome("Race car"); // false
```

In how many ways can you achieve this? I have 6 ways to Check if a given Word is a Palindrome. I know you will like to check them out.

This article will focus on ``word palindrome`` only. In a future episode, we will be looking at [sentence palindrome](https://dev.to/ebereplenty/algorithm-101-3-ways-to-check-if-a-sentence-is-a-palindrome-443d).

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

### Let's Check if a Word is a Palindrome using:

* toLowerCase(), split(), reverse(), join(), if...statement;

```javascript
      function wordPalindrome(word) {
        let lowerCasedWord = word.toLowerCase();
        let newWord = lowerCasedWord
          .split("")
          .reverse()
          .join("");

        if (newWord === lowerCasedWord) {
          return true;
        }
        return false;
      }
```

* toLowerCase(), spread operator, reverse(), join(), if...statement;

```javascript
      function wordPalindrome(word) {
        let lowerCasedWord = word.toLowerCase();
        let newWord = [...lowerCasedWord].reverse().join("");

        if (newWord === lowerCasedWord) {
          return true;
        }
        return false;
      }
```

* toLowerCase(), spread operator, if...statement

```javascript
      function wordPalindrome(word) {
        let lowerCasedWord = word.toLowerCase();
        let newWord = [...lowerCasedWord].reduce((total, acc) => acc + total);

        if (newWord === lowerCasedWord) {
          return true;
        }
        return false;
      }
```

* toLowerCase(), for...loop, join(), if...statement;

```javascript
      function wordPalindrome(word) {
        let lowerCasedWord = word.toLowerCase();
        let newWord = [];

        for (let i = lowerCasedWord.length; i >= 0; i--) {
          newWord.push(lowerCasedWord[i]);
        }

        if (newWord.join("") === lowerCasedWord) {
          return true;
        }
        return false;
      }
```

* toLowerCase(), for...of...loop, if...statement;

```javascript
      function wordPalindrome(word) {
        let lowerCasedWord = word.toLowerCase();
        let newWord = "";

        for (char of lowerCasedWord) {
          newWord = char + newWord;
        }

        if (newWord === lowerCasedWord) {
          return true;
        }
        return false;
      }
```

* toLowerCase(), for...in...loop, if...statement;

```javascript
      function wordPalindrome(word) {
        let lowerCasedWord = word.toLowerCase();
        let newWord = "";

        for (char in lowerCasedWord) {
          newWord = lowerCasedWord[char] + newWord;
        }

        if (newWord === lowerCasedWord) {
          return true;
        }
        return false;
      }
```

### Conclusion
There are many ways to solve problems programmatically. You are only limited by your imagination. 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 Find Hamming Distance](https://dev.to/ebereplenty/algorithm-101-3-ways-to-find-hamming-distance-45mo)

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.