Skip to content

Latest commit

 

History

History
26 lines (21 loc) · 708 Bytes

0009. Palindrome Number.md

File metadata and controls

26 lines (21 loc) · 708 Bytes

Screen Shot 2022-08-31 at 13 05 38

In javascript, checking if a number/string is a palindrome is easy, however... Follow up: Could you solve it without converting the integer to a string?

/**
 * @param {number} x
 * @return {boolean}
 */
var isPalindrome = function(x) {
    if(x < 0) return false;
    let num = x;
    let res = 0;
    
    while(num) {
        let last = num % 10;
        // take res as of type "string", but it's still a type of number
        res = res * 10 + last;
        num = Math.floor(num / 10);
    }
    return x === res
};