Solution in JavaScript using String object


  • 0

    I thought it was fun to use the String object as I don't get it to use much!

    /**
     * @param {number} x
     * @return {number}
     */
    var reverse = function(x) {
        const xString = new String(Math.abs(x));
        xString.sign = Math.sign(x);
        xString.reverse = 0;
        xString.split('').map((char, index) => {
            const digit = parseInt(char);
            if (digit > 0) {
                xString.reverse += digit * Math.pow(10, index)
            }
        });
        if (xString.reverse > 2147483647 ||  xString.reverse < -2147483648) return 0;
        return xString.reverse * xString.sign;
    };
    

Log in to reply
 

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