Infinite loop confusion, i+= 2 vs i+2

Hi all
I don’t understand why i+2 would create an infinite loop while i+=2 would not. I was under the impression they both did the same thing. When doing basic maths they seem to do the same thing. It is important that I understand why things happen and not just that they do as it’s easier to remember stuff when it makes sense.

// Setup
var myArray = [];

// Only change code below this line
for (var i=1; i<10; i +2) {
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36

Challenge: Iterate Odd Numbers With a For Loop

Link to the challenge:

i + 2 means that you take the value of i, add 2 to it… and do nothing with that value

i += 2 means that you take the value of i, add 2 to it… and save that result back into i

The loop body and loop termination condition are based on the value of i, so you need to change the value of i.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.