# Algorithm Problem Review # 6: Reverse the Number

This problem is found on LeetCode. I’ve done a lot of reversing strings and numbers so I thought this problem should be easy. It wasn’t…I found out here that reading closely is important even if you think you know what you’re doing.

# Problem 7: Reverse Integer

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).

Input: x = 123
Output: 321

Input: x = -123
Output: -321

Input: x = 120
Output: 21

Input: x = 0
Output: 0

## Constraints:

• `-2^31 <= x <= 2^31 - 1`
`function reverse(x){}`

# Understanding The Problem

The input I’m given is an integer, x. This function aims to return an integer that is a reverse of the original x input. Side note, the part that I missed and didn’t understand too much was the 32-bit range. So I will explain this part more later. This caused my function to fail the test a few times.

# Breaking it Down

Laying out my comments on what I need to do.

`function reverse(x){// do something// return the reversed number}`

“Do something”: My plan here was simple. I am very familiar with reversing strings so I decided why not turn this integer into a string and go from there. After turning it into a string, I will split it so it becomes and array of numbers. Use the reverse function to reverse that array. Then rejoin it back together with the join function. And finally return it back into an integer value with the parseInt function.

I may have a negative input value, so in order to deal with that I will make a variable that tells me if the value is negative or not. If it is, I will be sure to add the “-” to the beginning of the array before rejoining it together and turning it into an integer. If I didn’t do that, then the negative will make turning a string such as “321-” into an integer difficult.

`function reverse(x){// is x negative? default negative to be false  let negative = false// if statement to change the negative varible to true if x is a negative number  if (x < 0){    negative = true  }// turn the x into a string and split, reverse, join it together and turn it back into an integer number  x = x.toString().split("").reverse()// add the negative to the array if x was negative to start with  if (negative){    x.unshift("-")  }  x = parseInt(x.join(""))// return the reversed number  return x}`

This initially passed the simple test but failed the overall test on LeetCode. This is why there is an emphasis on understanding every part of the question so you can give the test what it wants.

## What is a 32-bit signed integer

This is an integer that is represented in 32 bits. Bits are binary (0s and 1s) so a 32-bit signed integer is an integer that is represented by a string of 32 (or less) 0s and 1s. In this case, the largest value the number can represent is 2³¹-1 and the smallest value is -2³¹.

So upon re-reading the question, if the reversed number value falls beyond the 32-bit integer range I should return 0 instead of the reversed number. To do this I will have an if statement to check if the reversed value falls beyond the 32-bit range, if so I will return 0.

The JavaScript Math.pow() function allows me to get the base to the exponent power of a number so I will use that to help me.

`function reverse(x){// is x negative? default negative to be false  let negative = false// if statement to change the negative varible to true if x is a negative number  if (x < 0){    negative = true  }// turn the x into a string and split, reverse, join it together and turn it back into an integer number  x = x.toString().split("").reverse()// add the negative to the array if x was negative to start with  if (negative){    x.unshift("-")  }  x = parseInt(x.join(""))// checking if the value is beyond the 32-bit range and returning 0 if so.  if (x > (Math.pow(2, 31) - 1) || x < Math.pow(-2,31)){    return 0  }// return the reversed number  return x}`

# SOLVED — O(n)

`function reverse(x){  let negative = false  if (x < 0){    negative = true  }  x = x.toString().split("").reverse()  if (negative){    x.unshift("-")  }  x = parseInt(x.join("")  if (x > (Math.pow(2, 31) - 1) || x < Math.pow(-2,31)){    return 0  }  return x}`

# My Thoughts + Refactoring

I learned a lot about bytes and bits with this question. Also learned a new JS Math function: Math.pow(base, element) which came in handy. After doing some research I found a better was to return the negative to the integer after turning it into a string and reversing it.

Instead of having more variables and checking if a number is positive or negative, I can use the JS Math function: Math.sign(number). This returns -1 if the number is negative or returns 1 if the number is positive. I can just multiple the reversed number by this returned value to get a negative number negative again after losing the negative sign from stringifying and returning to a number. In fact this makes the code look much smaller and neater.

`function reverse(x){//create a new variable xReversed split, reverse, join and return it to a number in one line. Then multiply it by the sign of the input x.let xReversed = parseInt(x.toString().split("").reverse().join("")) * Math.sign(x)if (xReversed > (Math.pow(2, 31) - 1) || x < Math.pow(-2,31)){    return 0  }return xReversed}`