Solution in javascript - 8 lines of code


  • 0
    S

    This was my solution in javascript.

    /**
     * @param {number} x
     * @return {number}
     */
    var reverse = function(x) {
        if(parseInt(x) === 0 || parseInt(x) == 'NaN') return 0;
        let result = (x > 0) ? '' : '-';
        while(x >= 1 || x <= -1 ) {
            x = parseInt(x);
            result += Math.abs(x%10);
            x = x/10;
        }
        result = parseInt(result);
        return (result >> 0 === result) ? result : 0;
    };
    

Log in to reply
 

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