- 3

Why I m getting 16 here instead of 6?

Why this comes to 16? Var x = 1 For (var i =0; I <6 ; I++) X+=i; Document.write(x)

1st Mar 2022, 3:37 PM
Evgeny Sergeev
Evgeny Sergeev - avatar
2 odpowiedzi
+ 3
<x> = 1 for...loop goes round 6 times, from 0 to 5 Iteration 1: <x> += 0 => 1 Iteration 2: <x> += 1 => 2 Iteration 3: <x> += 2 => 4 Iteration 4: <x> += 3 => 7 Iteration 5: <x> += 4 => 11 Iteration 6: <x> += 5 => 16
1st Mar 2022, 3:42 PM
Ipang
+ 2
If you want to print 6 then write x += 1 instead of x+= i
1st Mar 2022, 4:00 PM
NonStop CODING
NonStop CODING - avatar