Skip to content
Advertisement

Reverse an integer in JavaScript

I have written the following code to reverse an integer in JavaScript. It works fine but returns a 0 when given an input of -900000. Does anyone know what could be wrong?

JavaScript

Advertisement

Answer

You don’t need to slice off the - sign from negative numbers, you’ve already taken the absolute value as x. Drop that loop that moves digits around and destroys your values.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement