# How would a Tired Dev reverse an integer in JavaScript...

## and why?

Gideon Idowu

Published on Mar 21, 2021

Hey there ðŸ‘‹ Tired Dev here, how are you today? Hanging in there? Awesome! So am I ðŸ˜©.

I quickly want to break down the question of reversing an integer, for devs who may come across such a question in the future or who have come across it and didn't get the trick to solving it. Learned it and want to share my understanding of it.

The Question:

``````Given an integer, return an integer that is the reverse ordering of numbers.
--- Examples
reverseInt(16) === 61
reverseInt(971) === 179
reverseInt(300) === 3
reverseInt(-19) === -91
reverseInt(-80) === -8
``````

Things to note here:

• For negative numbers, the sign didn't change, only the number placement did.
• For numbers with zeros(0) behind them, the zeros disappeared when they were reversed.

#### The trick to solving this:

Know how to:

• store the sign of an integer, be it positive or negative,
• convert a number to a string (in JS),
• reverse a string,
• convert a string to a number.

That's it!

Let's take it piece by piece, shall we?

First, we define our function.

``````function reverseInt(x){

}
``````

Secondly, we store the sign of whatever integer value we recieve. How do we do this? Using the Math.sign() method in JavaSacript.

What's Math.sign? Never heard of that one...

Relax...

According to MDN

The Math.sign() function returns either a positive or negative +/- 1, indicating the sign of a number passed into the argument. If the number passed into Math.sign() is 0, it will return a +/- 0. Note that if the number is positive, an explicit (+) will not be returned.

``````function reverseInt(x){
const sign = Math.sign(x);
}
``````

We're now storing the sign of the integer value. That's one down.

Thirdly, convert a number to a string...

We make use of the toString() method that numbers possess in JS.

``````function reverseInt(x){
const sign = Math.sign(x);
const numString = x.toString();
}
``````

Easy right? That's two down, two to go! ðŸ”¥

Now we need to reverse the string, and the beautiful thing is, there are JS helper methods to do this! Here's a playful rhyme for when you need to remember how to reverse a string:

Whaddaya do when you want it in reverse? ðŸŽµ

I split the wood

I put the car in reverse

and I join the choir!

``````function reverseInt(x){
const sign = Math.sign(x);
const numString = x.toString();
const reversedNumString = numString.split('').reverse().join('');
}
``````

And with that, you'll always know the methods to use! Don't forget to add quotes with no spaces between them in the split and join methods. This tells JS to split the string everywhere it finds a space of that size, same for the join. Omitting it would get the whole word returned to you without it being split or joined! ðŸ˜“

To recap, we have stored the sign of the number, changed it to a string, and reversed the string. What's next? To return our newly reversed string to a number and reunite it with its integer sign, be it positive or negative.

How do we do that? Introducing JavaScript's own, parseInt !

``````function reverseInt(x){
const sign = Math.sign(x);
const numString = x.toString();
const reversedNumString = numString.split('').reverse().join('');
const  newlyReversedNum = parseInt(reversedNumString);
}
``````

With the parseInt method, which converts a string to a number for us, we now have our number back but in reversed form. And for the final piece...

``````function reverseInt(x){
const sign = Math.sign(x);
const numString = x.toString();
const reversedNumString = numString.split('').reverse().join('');
const  newlyReversedNum = parseInt(reversedNumString);

const reversedNumWithSign = newlyReversedNum * sign;

return reversedNumWithSign;
}
``````

This will multiply our reversed number either with 1, -1 or 0, depending on the sign and integer that was initially passed to it. I broke this down step by step, for ease of understanding. A shorter, more concise form would be:

``````function reverseInt(x){
const sign = Math.sign(x);
const reversedNumString = n.toString().split('').reverse().join('');
return parseInt(reversedNumString) * sign;
}
``````

And there you have it! You know how to reverse an integer, and you understand how every part of the process works.

What are the use cases of such a function and why would recruiters bother with such a question?

Well, one I could think of is an educational application for kids and adults. They may be required to type in the inverse of a number, just like we have it in Duolingo. Could you think of more use cases? I'd love to see your comments below!

Want to learn more stuff like this? I hear Stephen Grider has an Algorithm's course ðŸŒš...

I hope this was helpful, now this Tired Dev needs to rest, away with you!

PS: Photo by jose aljovin on Unsplash

Â