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
- Declare
resultwithlet, starting at 0. It builds the reversed number. Forn = 0the loop never runs, and 0 is the right answer. - Loop while
n > 0. The last digit comes off first, and it becomes the first digit of the reversed number. - Append the digit: set
resulttoresult * 10 + n % 10. Multiplication and%bind tighter than+, so no parentheses are needed. - Drop the digit from
n:n = Math.floor(n / 10). The digit is already safe inresult. - Return
resultafter 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:
| Pass | n | result |
|---|---|---|
| 1 | 12 | 3 |
| 2 | 1 | 32 |
| 3 | 0 | 321 |
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 % 10is 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, andreverseNumber(120)returns 21. - Zero and one digit.
reverseNumber(0)returns 0 andreverseNumber(7)returns 7. - The largest inputs.
reverseNumber(1000000000)returns 1, andreverseNumber(1234567890)would return 987654321. Reversed values stay far below JavaScript's exact-integer limit of 2^53. - Dropping the digit before using it. If
nis changed first,n % 10reads the wrong digit. Append the digit, then drop it. - Swapping the multiplication. Writing
result + n % 10 * 10shifts the new digit and not the digits already collected, which scrambles the number. - Forgetting
Math.floor.123 / 10is 12.3, and the fractional part then gets mixed into the later digits.
Testing
n = 123gives321.n = 120gives21.n = 7gives7.n = 0gives0.n = 1000gives1.
