loading...

Numerical Palindrome in JavaScript

twitter logo ・1 min read

A palindrome is a word, number, or sequence of characters which reads the same backward as forward. In this coding challenge we need a solution that tells if a given number is a palindrome. Only numbers allowed. For example: 2442 or 100001.

function isPalindrome(num) {
    if (typeof num !== 'number' || num < 0) {
        return false;
    }

    return +num.toString().split('').reverse().join('') === num;
}

console.log(isPalindrome(2442)); // true
console.log(isPalindrome(1234)); // false
console.log(isPalindrome(100001)); // true

Video going through this example:

twitter logo DISCUSS
Classic DEV Post from Jun 5 '19

GitHub Just Released a New Version of Their Desktop App

coder4life profile image