The Digit Reverser

EasyAcc. 96.1%
+20 XP 8

The Mirror Logic

Reversing a number involves "peeling off" the last digit and "pasting" it onto the end of a new number. Example: $425 o$ extract $5 o (0 imes 10 + 5) = 5 o$ extract $2 o (5 imes 10 + 2) = 52 o dots$

The Assignment

Your function receives a parameter named num.

  1. Initialize reversed to 0.
  2. Use a while loop that continues while num is not 0.
  3. In each step:
    • Extract the last digit (num % 10).
    • Multiply your current reversed by 10 and add the extracted digit.
    • Remove the last digit from num (Math.floor(num/10)).
  4. After the loop, print the reversed value.

01EXAMPLE 1

Inputnum = 123
Output321

Explanation: Math-based reversal.

Constraints

  • Strictly avoid converting to String.
  • Must use a while loop.
MathLoopsDigits
JavaScript
Loading...
3 Hidden

Input Arguments

num123

Expected Output

321

Click RUN to test your solution