0

javascript question to a challenge

var i = 12; (++i) % 3; alert(i); my solution was: 1 the right solution was : 13; but why? can someone plz explain to me why 13?

13th Oct 2020, 9:12 PM
Mylisa_beth
Mylisa_beth - avatar
3 Respostas
+ 3
Because you've increment i of one so i is 13 If the question was : var i = 12 i = (++i) % 3 alert(i) The solution is 1
13th Oct 2020, 9:17 PM
Yannis_Sauzeau
Yannis_Sauzeau - avatar
+ 1
this look like a little trick question, (++i) % 3; actually do nothing except increment the variable i. So you thought that the result was 13 % 3 = 1, but this result isn't being stored anywhere. The alert only print the new i wich is now 13.
13th Oct 2020, 9:16 PM
Apollo-Roboto
Apollo-Roboto - avatar
0
thank you! 😊 my mistake. 🤭
14th Oct 2020, 6:03 AM
Mylisa_beth
Mylisa_beth - avatar