Wondering what's wrong with my Javascript code


  • 0
    W

    I get confused about my code. I can test that reverse value is equal to input x, but when I want to return the boolean value of "x ==reverse", it returns false; I don't know why?

    var isPalindrome = function(x) {
        if((x < 0)){return false;}
        var reverse = 0;
        
        while(x !== 0 ){
            reverse = reverse * 10 + x % 10;
            x = Math.floor(x/10);
        }
        
        
        return x == reverse;
       
    };
    

  • 0

    Because after your while loop, x is 0.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.