Low-level integer numbers reversing:
function flipInt(n){
var digit, result = 0
while( n ){
digit = n % 10 // Get right-most digit. Ex. 123/10 β 12.3 β 3
result = (result * 10) + digit // Ex. 123 β 1230 + 4 β 1234
n = n/10|0 // Remove right-most digit. Ex. 123 β 12.3 β 12
}
return result
}
// Usage:
alert(
"Reversed number: " + flipInt( +prompt("Enter a value") )
)
Run code snippetEdit code snippet Hide Results Copy to answer Expand
The above code uses bitwise operators for quick math
This method is MUCH FASTER than other methods which convert the number to an Array and then reverse it and join it again. This is a low-level blazing-fast solution.
Illustration table:
Show code snippet
const delay = (ms = 1000) => new Promise(res => setTimeout(res, ms))
const table = document.querySelector('tbody')
async function printLine(s1, s2, op){
table.innerHTML += `<tr>
<td>
{s2||''}</td>
</tr>`
}
async function steps(){
printLine(123)
await delay()
printLine('12.3 β')
await delay()
printLine(12, 3)
await delay()
printLine('1.2', '3 × 10')
await delay()
printLine('1.2 β', 30)
await delay()
printLine(1, 32)
await delay()
printLine(1, '32 × 10')
await delay()
printLine('1 β', 320)
await delay()
printLine('', 321)
await delay()
}
steps()
table{ width: 200px; }
td {
border: 1px dotted #999;
}
<table>
<thead>
<tr>
<th>Current</th>
<th>Output</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Answer from vsync on Stack OverflowVideos
What is the easiest way to reverse a number in JavaScript?
Can I use recursion to reverse a number in JavaScript?
How to reverse a number in JavaScript using a for loop?
Low-level integer numbers reversing:
function flipInt(n){
var digit, result = 0
while( n ){
digit = n % 10 // Get right-most digit. Ex. 123/10 β 12.3 β 3
result = (result * 10) + digit // Ex. 123 β 1230 + 4 β 1234
n = n/10|0 // Remove right-most digit. Ex. 123 β 12.3 β 12
}
return result
}
// Usage:
alert(
"Reversed number: " + flipInt( +prompt("Enter a value") )
)
Run code snippetEdit code snippet Hide Results Copy to answer Expand
The above code uses bitwise operators for quick math
This method is MUCH FASTER than other methods which convert the number to an Array and then reverse it and join it again. This is a low-level blazing-fast solution.
Illustration table:
Show code snippet
const delay = (ms = 1000) => new Promise(res => setTimeout(res, ms))
const table = document.querySelector('tbody')
async function printLine(s1, s2, op){
table.innerHTML += `<tr>
<td>
{s2||''}</td>
</tr>`
}
async function steps(){
printLine(123)
await delay()
printLine('12.3 β')
await delay()
printLine(12, 3)
await delay()
printLine('1.2', '3 × 10')
await delay()
printLine('1.2 β', 30)
await delay()
printLine(1, 32)
await delay()
printLine(1, '32 × 10')
await delay()
printLine('1 β', 320)
await delay()
printLine('', 321)
await delay()
}
steps()
table{ width: 200px; }
td {
border: 1px dotted #999;
}
<table>
<thead>
<tr>
<th>Current</th>
<th>Output</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Assuming @DominicTobias is correct, you can use this:
console.log(
+prompt("Enter a value").split("").reverse().join("")
)
Run code snippetEdit code snippet Hide Results Copy to answer Expand
You do not return the result. Use
return num.split("").reverse().join("");
If you use just return, then undefined is returned.
function reverse_the_string(num) {
return String(num).split("").reverse().join("");
}
document.write(reverse_the_string(56789));
Caveat: Reversing works only with small numbers!
The solution of casting the number into a string and reversing the string is a good solution, BUT it doesn't cover all the cases. There are 2 cases that need to be addressed:
- Reversing a negative number it doesn't retain the sign.
- Reversing a number that ends with zero(s), after reversing you will have leading zeros, e.g. 12300 the reverse will be "00321" when it should be "321".
Using Regular function
var reversed = parseFloat(num.toString().split("").reverse().join()) * Math.sign(num);
Using Arrow Function
var reversed = num => parseFloat(num.toString().split('').reverse().join('')) * Math.sign(num);