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.
- Initialize
reversedto 0. - Use a
whileloop that continues whilenumis not 0. - In each step:
- Extract the last digit (
num % 10). - Multiply your current
reversedby 10 and add the extracted digit. - Remove the last digit from
num(Math.floor(num/10)).
- Extract the last digit (
- After the loop, print the
reversedvalue.
01EXAMPLE 1
Input
num = 123Output
321Explanation: Math-based reversal.
Constraints
- Strictly avoid converting to String.
- Must use a while loop.
MathLoopsDigits
JavaScriptSystem handles I/O — write your function only
Loading...
3 Hidden
Input Arguments
num123
Expected Output
321
Click RUN to test your solution