# JavaScript Reverse Integer Walkthrough With Recursion

I’m currently learning about recursions, which is when a function calls on itself instead of using a loop. Recursions have the potential to run forever and crash, which is why it is important to remember to include a base case, or a condition that when met, stops the recursion. In this blog post I will be reviewing and unpacking the solution to a LeetCode problem. Hopefully you will walk away with a better understanding of how to approach this or other similar problems in the future. As my ongoing journey of becoming a master developer continues, I have found that bringing mindfulness to the patterns of code, makes a huge difference in reaching my learning goals. I hope that in sharing my thought process, I can impart that wisdom to others as well.

## Let’s look at the problem

“Reverse Integer”. The task: “given a 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range, then return 0.”

Examples:

Input: x =112

Output: 211

Input: x = -130

Output: -31

Input: x = 0

Output: 0

Input: x = 2147483649 (2 to the power of 31 – 1)

Output: 0

So breaking this down to understand the question, we are creating a function that takes in an integer, ‘x’ as an argument. The function reverses the order of the numbers within the integer so that the output is the integer starting from the last digit and ending with the first digit of ‘x’. The only condition is that if the number is greater than 2 to the power of 31 minus 1, or less than negative 2 to the power of 31, then we return 0.

## So let’s write out our function:

The first thing we want to do is think through the problem. Write out what the function will look like in the inside using sudo code and see what we need from there. We know that we want our solution to be the reverse of an integer so we need a way to reverse an integer. If we convert the integer to a string, then use the built in string method `.split(‘’)`

then the built in array method `.reverse()`

, then we join the reversed array with the built in array method `.join(‘’)`

, we take care of a large part of the code.

`const reverse = (x) => {`

const solution = (x+"").split('').reverse().join('')

return solution

};

The next thing we need to do is set up the conditions so that our solution doesn’t surpass 2 to the power of 31 minus one or go below negative 2 to the power of 31. We’ll create our condition just before we return our solution. We will say that if the solution is greater than 2 to the power of 31 minus 1 that we will return 0. This just leaves us with one more problem.

`const reverse = (x) => {`

const solution = (x+"").split('').reverse().join('')

if (solution > 2**31 -1) {

return 0

}

return solution

};

Great. We have a condition that catches our solution and returns 0 if the value goes beyond a certain number, but what if x is equal to a negative number? We need a condition to cover the lowest value. To do that, I’ve gone the route of implementing a recursion. Before the function get’s to declare a solution, I want ‘x’ to first go through a condition where we test if it is less than 0, in which case, ‘x’ is a negative integer this condition will be met. If ‘x’ is a negative integer, we will return` -1 * reverse(-x)`

.

`const reverse = (x) => {`

if (x < 0) return -1 * reverse(-x)

const solution = (x+"").split('').reverse().join('')

if (solution > 2**31 -1) {

return 0

}

return solution

};

## How does this work?

If x is a negative number, we will recursively call our reverse function with a negative value of `x,`

which is already negative. With this new value being a double negative, `x`

will be a positive value that then bypasses the first condition and we will then get our reversed solution.

So if x = -345, here is what is happening:

`let x = -345 `

reverse(x)

//=> -345 < 0 is true

//=> return -1 * reverse(-(-345))

//=> reverse(345)

//=> solution

//"345" => ['3', '4', '5'] => ['5', '4', '3'] => 543

//=> return -1 * solution

//=> -543

And there you have an implementation of recursion to find the reversed value of a negative integer. I hope this was helpful.