Today I've decided to tackle LeetCode's problem #7, Reverse Integer. The guidelines state the following.

Given a signed 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`[-2^31, 2^31 - 1]`

, then return`0`

.

Assume the environment does not allow you to store 64-bit integers (signed or unsigned).

That's an interesting variant of reversing something. I've seen strings before, and that's simple using built in JS methods like `split()`

, `reverse()`

, and `join()`

. But reversing an integer? Might be a bit difficult, considering that the problem is marked medium and has a 27.3% success rate.

But never fear, it's not impossible. Let's break it down into steps. There are two main ones that pop out at me immediately - checking if the number is negative and checking if it, reversed, goes out of the range that we are told to stay inside. We'll tackle the first problem first - determining if the number is negative.

This is pretty simple - to start, I coerced the number into a string and split it into an array. You'll see why in a minute.

```
let arr = x.toString().split();
```

That's pretty tidy. Now let's check if the number is negative or not. Originally I used `includes('-')`

, but after some benchmarking I determined that since we know where the sign would be anyway, why not just explicitly check the first element of the array with `arr[0] == '-'`

? So add the following logic.

```
if (arr[0] == '-') {
// if it's negative
} else {
// if it's not
}
```

Let's tackle the second case first - what happens if it's not negative? We'll do two things - get the actual value (recall currently it's an array of strings, not an integer), and then check if it is in the bounds that we have been set. Here's the logic.

```
let num = parseInt(arr.reverse().join(''), 10);
return num > Math.pow(2, 31) ? 0 : num;
```

Allow me to explain. In the first line, we join the array of strings into a single string which we then parse an integer from. That's our reversed number.

But what about the constraints? Since we know the number is positive, we only need to check if it's less than 2^31. Enter `Math.pow()`

. We'll use a ternary operator to check how big our value is, and return `0`

if it's larger and `num`

if it's smaller. Pretty cool, right?

So that's positive. And negative actually isn't that different. The only differences are that 1), we need to worry about the negative sign when we reverse and 2), that we check for a different range (`-2^31`

). So let's implement that!

```
arr.unshift();
let num = parseInt('-' + arr.reverse().join(''), 10);
return num < Math.pow(-2, 31) ? 0 : num;
```

In this logic, we 1) remove the negative sign from the array with `unshift()`

, 2) get the number and reverse it but add a negative sign at the front, and 3) return the same way as before but checking with the different constraint (`-2^31`

).

So the final code looks something like this.

```
let arr = x.toString().split('');
if (arr[0] == '-') {
arr.unshift();
let num = parseInt('-' + arr.reverse().join(''), 10);
return num < Math.pow(-2, 31) ? 0 : num;
} else {
let num = parseInt(arr.reverse().join(''), 10);
return num > Math.pow(2, 31) ? 0 : num;
}
```

As always, all code in this blog can be found in the Github repository.

Happy coding!

## Top comments (0)