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); }
3 Respostas
+ 3
The i -= 2 is it done before or after the i <= depth. I think before.
+ 3
Shouldn't it be:
if (i >= depth) {
break;
}
+ 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?