DEV Community

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

Posted on • Updated on

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

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 every

  • nice solution in case array methods are not permitted by the interviewer.
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 πŸ‘πŸ˜Š

Discussion (0)