Algorithms and Data Structures – ReversedInt

--- 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) === -51
reverseInt(-90) === -9
Solution
Hint: Try to use Math.sign(n) -> when it get a negative value, it will return -1, otherwise it will return 1 or 0
1
2
3
4
5
6
7
8
 const reversed = n
        .toString()
        .split('')
        .reversed()
        .join('');
    return parseInt(reversed) * Math.sign(n);
}
cs

Leave a Comment

ko_KRKorean