Start learning free

LESSON 213 OF 285 · WHILE LOOPS - CHALLENGES

Reverse a Number in JavaScript: Building It Digit by Digit

Practice Read the full explanation

Given a non-negative integer n, return the number you get when you write its digits in reverse order.

Zeros at the end of n move to the front of the reversed number, where they don't count: 120 reversed is 21. If n is 0, the answer is 0.

 

Example 1:

Input: n = 123
Output: 321
Explanation: The digits 1, 2, 3 read backwards are 3, 2, 1.

Example 2:

Input: n = 120
Output: 21
Explanation: Backwards the digits are 0, 2, 1, and 021 is the number 21.

Example 3:

Input: n = 7
Output: 7
Explanation: A number with one digit stays the same.

 

Constraints:

  • 0 <= n <= 109
STEP 1 OF 5
    Ctrl + Enter to check

    JAVASCRIPT · WHILE LOOPS - CHALLENGES

    Reverse Number in JavaScript, explained

    Problem overview

    The method reverseNumber receives a non-negative integer n and returns the number you get by writing its digits in reverse order. 123 becomes 321. Trailing zeros move to the front of the reversed number, where they don't count, so 120 becomes 21. A one-digit number stays the same, and 0 gives 0.

    Approach: move digits from one number to another

    You can take the last digit off n with n % 10 and drop it with Math.floor(n / 10). Moving digits one at a time from the end of n onto the end of a new number, result, reverses them: the digit taken first ends up leftmost.

    To append a digit to the end of a number, shift the number one place left by multiplying by 10, then add the digit. If result is 32 and the next digit is 1, then 32 * 10 + 1 is 321. The order is already right, with no extra step needed to turn it around.

    Step by step

    1. Declare result with let, starting at 0. It builds the reversed number. For n = 0 the loop never runs, and 0 is the right answer.
    2. Loop while n > 0. The last digit comes off first, and it becomes the first digit of the reversed number.
    3. Append the digit: set result to result * 10 + n % 10. Multiplication and % bind tighter than +, so no parentheses are needed.
    4. Drop the digit from n: n = Math.floor(n / 10). The digit is already safe in result.
    5. Return result after the loop.

    The solution in JavaScript

    class Solution {
        reverseNumber(n) {
            let result = 0;
            while (n > 0) {
                result = result * 10 + n % 10;
                n = Math.floor(n / 10);
            }
            return result;
        }
    }
    

    For reverseNumber(123), the values after each pass are:

    Passnresult
    1123
    2132
    30321

    For reverseNumber(120), the passes give result as 0, then 2, then 21. The first digit moved is the final 0 of 120, and 0 * 10 + 0 is still 0. That is why trailing zeros vanish: a number has no leading zeros.

    • The result is a number, not a string. A string such as "021" would keep its zero, but a number can't. This is the behaviour the lesson wants.
    • Precedence. result * 10 + n % 10 is read as (result * 10) + (n % 10), because * and % have the same rank and bind tighter than +.

    How many passes

    Time: O(d), one pass per digit, where d is the number of digits of n (at most 10 here). Space: O(1).

    The digit loop here has the same shape as the loops that sum or count digits: test the last digit with %, then drop it with Math.floor. Only the line in the middle changes.

    Edge cases and common mistakes

    • Trailing zeros. reverseNumber(1000) returns 1, and reverseNumber(120) returns 21.
    • Zero and one digit. reverseNumber(0) returns 0 and reverseNumber(7) returns 7.
    • The largest inputs. reverseNumber(1000000000) returns 1, and reverseNumber(1234567890) would return 987654321. Reversed values stay far below JavaScript's exact-integer limit of 2^53.
    • Dropping the digit before using it. If n is changed first, n % 10 reads the wrong digit. Append the digit, then drop it.
    • Swapping the multiplication. Writing result + n % 10 * 10 shifts the new digit and not the digits already collected, which scrambles the number.
    • Forgetting Math.floor. 123 / 10 is 12.3, and the fractional part then gets mixed into the later digits.

    Testing

    • n = 123 gives 321.
    • n = 120 gives 21.
    • n = 7 gives 7.
    • n = 0 gives 0.
    • n = 1000 gives 1.
    Keep going with JavaScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free