Reverse Integer
The first thing that crossed my mind in regards to this new challenge was: “Hmm that sounds like ‘reverse a string,’ seems pretty simple” and then when I started working on it, I was like “No, it’s not that simple!”
There can be many possible cases of “reverse an integer,” depending on the description of the challenge.
Let’s say the most basic description is just an instruction to reverse 123 to 321. We can solve it in one line:
function reverse(n) {
return parseInt(n.toString().split('').reverse().join())
}reverse(123)
//expected output: 321
Let’s break down what happened above:
- Convert the integer to a string first, and use a split to turn it to an array. Then, using the reverse method we reverse the order of each element in the array. After that we can join it to the string.
- Don’t stop at returning the string right away. They give us an integer so we just need parseInt to return an integer.
The next challenge is: what if they give us a negative integer and we are not allowed to skip that fact? For this challenge, we need one more function which is Math.sign() and this function returns the sign of a number, indicating whether the number is positive, negative or zero.
For example:
console.log(Math.sign(1000));
// expected output: 1console.log(Math.sign(-3342));
// expected output: -1console.log(Math.sign(0));
// expected output: 0
Now all we need to do is multiply the result we got from the previous challenge with Math.sign(number):
function reverse(n) {
return parseInt(n.toString().split('').reverse().join()) * Math.sign(n)
}reverse(-123)
// expected output: -321
You can split the function into multiple lines or just leave it in one line like that.
Get Linh Huynh’s stories in your inbox
Join Medium for free to get updates from this writer.
There are multiple ways to solve this challenge, you can also use while loop to solve it:
function reverse(n) {
let result = 0
while (n!=0) {
result = result * 10 + n % 10
n = parseInt(n/10)
}
return result
}reverse(-123)
//expected output: -321
- The first loop’s result will be 3, which is the remainder of -123%10. After that we will divide the number by 10, and round it to the nearest whole number. Here we are using parseInt instead of Math.floor() because parseInt() will work with negative numbers. n will be -12.
- The second loop’s result will be 32 which gets its remainder from -12%10. n will be -1.
- Last loop (oh yes we’re almost done!!): As you can guess, the result will be 321 from -1%10 and n will be 0.
- With the condition n!=0, if the number is 0, the result will be 0.
Hold on! That’s not the end yet. What if there is another requirement such as this one from Leetcode:
It may sound complicated as the description is pretty long but there is a function that works perfectly for this challenge: Math.pow(), this function returns the base to the exponent power.
We can use the solution above and add one condition based on the requirement:
function reverse(n) {
let result = 0
while (n!=0) {
result = result * 10 + n % 10
n = parseInt(n/10)
}
if ( r < Math.pow(-2, 31)) || (r > Math.pow(2, 31) - 1){
return 0
}
return result
}reverse(1563847412)
//expected output: 0
We can also use Math.abs to turn the result to absolute value and make our code a little bit shorter:
if (Math.abs(r) > Math.pow(2, 31)) {
return 0;
}There are a lot of solutions for this challenge, if you have different ones, please let me know!








