Reverse: Integer


Directions

  • Given an integer, return an integer that is the reverse ordering of numbers.
  • examples:
reverseInt(15) //--> 51
reverseInt(981) //--> 189
reverseInt(500) //--> 5
reverseInt(-15) //--> -15
reverseInt(-90) //--> -9

Solutions

JS

My Solution

function reverseInt(n) {
    const reversed = n
        .toString()
        .split('')
        .reduce((acc, cur) => cur + acc, '');

    return Math.sign(n) * parseInt(reversed);
}

SG Solution 1

function reverseInt(n) {
    const reversed = n
        .toString()
        .split('')
        .reverse()
        .join('');

    return Math.sign(n) * parseInt(reversed);
}
Made with Gatsby G Logo