JS Coding Question #3: Is Palindrome [Common Question]
Let's Code

Let's Code @frontendengineer

About: A fan of technology/innovation, software engineer, and a father.

Joined:
May 6, 2019

JS Coding Question #3: Is Palindrome [Common Question]

Publish Date: Aug 27 '21
18 6

Interview Question #3:

Write a function that returns if string a palindrome❓🤔
Palindrome happens when a string forms the same word when it is reversed.

Example:
abba => true
abcba => true
123xyz => false

If you need practice, try to solve this on your own. I have included 2 potential solutions below.

Note: There are many other potential solutions to this problem.

Feel free to bookmark 🔖 even if you don't need this for now. You may need to refresh/review down the road when it is time for you to look for a new role.

Code: https://codepen.io/angelo_jin/pen/RwgPVwr

Solution #1: Array reverse and join and methods

  • very straight forward solution using array methods.
function isPalindrome(str) {
  return str
    .split('')
    .reverse()
    .join('') === str;
}
Enter fullscreen mode Exit fullscreen mode

Solution #2: Array forEach

  • iterative solution that avoids array reverse() and join() methods
function isPalindrome(string) {
    // find the length of a string
    const len = string.length

    // loop through half of the string
    for (let i = 0; i < len / 2; i++) {

        // check if first and last string are same
        if (string[i] !== string[len - 1 - i]) {
            return false
        }
    }
    return true
}
Enter fullscreen mode Exit fullscreen mode

Solution #3: Array every

  • another iterative solution and is shorter version.
function isPalindrome(str) {
  return str.split('').every((char, i) => {
    return char === str[str.length - i - 1]
  })
}
Enter fullscreen mode Exit fullscreen mode

Happy coding and good luck if you are interviewing!

If you want to support me - Buy Me A Coffee

In case you like a video instead of bunch of code 👍😊

Comments 6 total

  • filip
    filipMar 17, 2022

    Hello my friend!
    One time I had this type of interview exercise, the only problem that it has to be made without using strings!
    It was only for numbers.
    For example, 121 is palindrome while 123 is not.
    It would be nice you put a solution without using strings, maybe to help others who reads this article.

    • Let's Code
      Let's CodeDec 23, 2022

      I apologize for overlooking this.

      a possible solution is that you can use is to convert the number to array right away and then adapt either my two solutions above.

      const numberToArray = (number) => {
          var arr = [];
          while(number>0) { arr.push(number%10); number = number/10|0; }
          return arr.reverse();
      }```
      
      
      Enter fullscreen mode Exit fullscreen mode
    • neillindberg
      neillindbergMar 6, 2023

      Just cast every input as a string with .toString() and it would work for both type string and number.

  • corebugcreator
    corebugcreatorMar 18, 2023

    Image description
    This recursive solution may be also asked about

    • Let's Code
      Let's CodeMar 23, 2023

      i like it. thanks for sharing

  • Vinayak Phal
    Vinayak PhalAug 13, 2023

    Another solution, start from both the ends.

    function isPalindrome(str) {
      if(str.length<=1){ return true; }
      let i=0;
      let j=str.length-1;
      while(i<j && str[i] === str[j]){
        i++;
        j--;
      }
      return i>=j;
    }
    
    Enter fullscreen mode Exit fullscreen mode
Add comment