+ 1

Need clarification on how the output of this code is 704. Many thanks!

var input = 407; var res = 0; for (i = input; i > 0; i = Math.floor(i/10)) { res = res * 10 + i % 10; } document.write(res)

22nd Feb 2018, 6:01 PM
Briaה‎
1 Odpowiedź
+ 2
Let's iterate through the loop iteration 1: i = 407 res = 0*10 + 7 i = 40 iteration 2: i = 40 res = 7*10 + 0 i = 4 iteration 3: i = 4 res = 70*10 + 4 i = 0 out of the loop res = 704
23rd Feb 2018, 7:55 AM
Dipendra Shrestha
Dipendra Shrestha - avatar