0

Snail on the well

Help! I can not understand why the algorithm is not executed? If i<=depth the loop is sometimes run again. What have I done wrong? function main() { var depth = parseInt(readLine(), 10); for (i=0, n=0 ; i <= depth; i-=2){ i+=7; n++; } console.log(n); }

20th Dec 2022, 1:54 PM
Чеслав Черный
Чеслав Черный - avatar
3 Réponses
+ 3
The i -= 2 is it done before or after the i <= depth. I think before.
20th Dec 2022, 5:03 PM
Paul
Paul - avatar
+ 3
Shouldn't it be: if (i >= depth) { break; }
21st Dec 2022, 6:27 PM
Paul
Paul - avatar
+ 1
Thank you! I added an "if" to the loop and it worked. ... if ( i <= depth) break; } ... Only nothing worked, if I left the "i <= depth" condition in the "for" loop, everything ended on the first run, why?
21st Dec 2022, 6:20 PM
Чеслав Черный
Чеслав Черный - avatar