15,304,161 members
Articles / Programming Languages / Javascript
Tip/Trick
Posted 26 Aug 2016

49.8K views
4 bookmarked

# How to Reverse a Number using JavaScript

Rate me:
Interview question: how do you reverse a number using JavaScript?

## Introduction

I recently had an interview and was asked to create a JavaScript function that would reverse a number (i.e., given `123`, the outcome is `321`). This was quite simple to me, so I answered "convert the number to a `string` and reverse the digits". Unfortunately, this was not the answer they were looking for. They wanted a solution that did not use any `string`s or (`string`) conversion (they wanted me to invent the wheel).

## Background

On the spot in an interview, I saw some beautiful symmetry in a solution for reversing a number. I knew right away this would involve the mod (`%`) operator and some looping.

## Using the Code

This solution can be used in client side programming to reverse a number.

JavaScript
```<script language='javascript'>
Reverse = function(number) {
var reversed = 0;

while (number != 0) {
reversed *= 10;
reversed += number % 10;
number -= number % 10;
number /= 10;
}

return reversed;
}
</script>

// Here's a simple way to test the function:

<body>
<input type='text' id='string'/>&nbsp;<input type='button' value='text'
</body>```

This little function has a simple loop that executes until the incoming parameter (number) becomes zero. Notice the symmetry between "`number`" and "`reversed`", inside the loop. The value of "`reversed`" (result) starts out at zero and keeps getting multiplied by `10` and then increased by the `number` mod `10`. The "`number`" (incoming value) keeps getting decreased by the `number` mod `10` and then is divided by `10`.

One member responded to our tip asked "what about reversing `12345.6789` into `9876.54321`"? This becomes challenging (dealing with a decimal), given the assumption "thou shalt not manipulate the string"! Here is my solution:

JavaScript
```Reverse = function(number) {
var reversed = 0;
var exponent = number.indexOf('.');

if (exponent !== -1) {
number *= Math.pow(10, number.length - exponent - 1);
}

while (number != 0) {
reversed *= 10;
reversed += number % 10;
number -= number % 10;
number /= 10;
}

if (exponent !== -1) {
reversed /= Math.pow(10, exponent);
}

return reversed;
}```

This snippet shows how we can first: adjust the (floating point) number to make it an integer value, then follow the same loop process as in the preceding snippet, and last adjust the reversed result for the decimal. We use `Math.pow()` with base `10` and an exponent to accomplish this.

## Points of Interest

I don't know why there would ever be a need to reverse a number but this was a fun problem to solve. Hope this article peaks your interest in client side programming and gets you thinking toward more elegant solutions!

## History

This is the initial revision.

## Share

 United States
No Biography provided

 First Prev Next
 reverse in single line JS code, try this aneeshchopra13-Jan-20 21:59 aneeshchopra 13-Jan-20 21:59
 TRY THIS ONE TO REVERSE NUMBER IN JAVASCRIPT Member 1237643313-Nov-17 2:42 Member 12376433 13-Nov-17 2:42
 Do not use indexOf -=at=-13-Jan-17 2:52 -=at=- 13-Jan-17 2:52
 Far too complicated... Kornfeld Eliyahu Peter31-Aug-16 9:04 Kornfeld Eliyahu Peter 31-Aug-16 9:04
 With decimals... Member 1219848029-Aug-16 15:37 Member 12198480 29-Aug-16 15:37
 Smart phil.o26-Aug-16 7:41 phil.o 26-Aug-16 7:41
 Last Visit: 31-Dec-99 18:00     Last Update: 19-May-22 11:25 Refresh 1