+ 4

what is answer for this loop and why?

var i = 1; for (k=1; k<10; k++) { i += k; }

30th Mar 2020, 5:06 PM
medzvre gena
medzvre gena - avatar
5 Answers
+ 1
var i = 1; for (k=1; k<10 ; k++) { i += k; } that would work 👍👍
21st Jul 2021, 6:55 PM
saad muaz
saad muaz - avatar
0
For what, you can try in code playground (4th button, with {} icon) For why, 1 + 1 + 2 + 3 +.... + 9
30th Mar 2020, 5:26 PM
Gordon
Gordon - avatar
0
d1spache thanks I just could not understand why the answer was 46 ;)
30th Mar 2020, 5:30 PM
medzvre gena
medzvre gena - avatar
0
output would be: 2 4 7 11 16 22 29 37 46 k=1 i=1 i = i + k; i = 1 + 1; = 2 k = 2; i = 2; i = i + k; i = 2 + 2; = 4 k = 3; i = 4; i = i + k; i = 4 + 3; = 7 k = 4; i = 7; i = i + k; i = 7 + 4; = 11 and so on
19th Mar 2022, 9:27 AM
Jan Augustin B. Talavera