Reverse an Integer--the Right Way
Tags: javascript, algorithms, integers, codenewbie
Published: 2020-02-12 22:37:08

A common algorithm problem is to reverse a positive integer: turn 12345 into 54321. At first glance, this problem may seem pretty easy--turn the integer into a string, then an array, reverse the array, and then turn that into a string, and finally into an integer again. While that solution may get you '54321', it's not the best way to do the problem.

If an algorithm's input value is an integer, and the expected output is an integer, then the best solution to the problem does not involve converting the integer to another data type. Instead, to solve the simple 'reverse the integer' problem, you should use modulo and simple math. (I will be writing the solution to this problem in JavaScript, but it can be tackled similarly in other languages.)

The first thing to do is write a function, which I'll call `reverseNum`, which takes in a number. Then, initialize a new variable, called `result` and set it equal to zero. The result is what you will return in the end:

```javascript

function reverseNum(num) {
    let result = 0

    //...

    return result
}

```

Now, you want to perform an action on the number until the input equals zero. To do that, create a while loop:


```javascript

function reverseNum(num) {
    let result = 0

    while (num > 0) {
        //...
    }

    return result
}

```

Inside of the while loop, the first thing you'll want to do is get the modulo of the number. The modulo operator (%) returns the remainder, so if you did something like `13%10`, the result would be 3, because the remainder of 13/10 is 3. In this algorithm, you want to repeatedly get the last value of the input number, which can be done using modulo:


```javascript

function reverseNum(num) {
    let result = 0

    while (num > 0) {
        let mod = num % 10
        //...
    }

    return result
}

```

Now that you have the modulo, you need to modify the original number so that that final value is no longer in the number. To do that, you'll have to divide the number by 10, and do Math.floor() on that operation:


```javascript

function reverseNum(num) {
    let result = 0

    while (num > 0) {
        let mod = num % 10
        num = Math.floor(num/10)
        //...
    }

    return result
}

```

Finally, you will want to modify the 'result' variable. You want to add 'mod' to the result, but each time you go through the while loop, you'll want to multiply the existing result by 10 so that it grows longer, rather than simply adding up all of values of the number:

```javascript

function reverseNum(num) {
    let result = 0

    while (num > 0) {
        let mod = num % 10
        num = Math.floor(num/10)
        result = result * 10 + mod
    }

    return result
}

```

And that's it! To test that this solution works, let's try the number '123' using a truth table:

| result        | mod           | num  |
| ------------- |:-------------:| -----:|
| 0      | 3 | 12 |
| 0*10 +3 = 3      | 2      |   1 |
| 3*10 +2 = 32 | 1      |    0 |
| 32*10 +1 = 321 |       |     |


Because num is now 0, the while loop no longer executes, and the function returns the result, which is 321.




-------------------------

If the input could be either positive or negative, you can easily modify this function by checking for that right at the start, and then adding a negative sign if necessary at the end.

First, instantiate a variable and set it equal to a boolean. If the input number is positive, there's no need to alter the variable. If it's negative, set that variable to 'false', and set the inputted number to the absolute value of itself:

```javascript

function reverseNum(num) {
    let pos = true
    if (num < 0) {
        pos = false
        num = Math.abs(num)
    }

    let result = 0

    while (num > 0) {
        let mod = num % 10
        num = Math.floor(num/10)
        result = result * 10 + mod
    }

    //...
}

```

Then, in the final return statement, you need to check if the 'pos' variable is true or false. If it's true, then simply return the result. If it's false, you know the inputted value was negative, so you return the result times -1:


```javascript

function reverseNum(num) {
    let pos = true
    if (num < 0) {
        pos = false
        num = Math.abs(num)
    }

    let result = 0

    while (num > 0) {
        let mod = num % 10
        num = Math.floor(num/10)
        result = result * 10 + mod
    }

    return pos ? result : (-1 * result)
}

```