Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Review For Loops and Loop Exiting

Why does this run 11 not 10 times ? for (var j = 0; j<=100; j += 10;); console.log(j);

To my mind the logic is the var is 0, its checked as less than 100 so 10 is added. the first log would be 10 not 0?

2 Answers

You code won't run with the semicolons but I get what you are asking. j += 10 adds 10 each time after the code in the for loop executes. So the initial value of 0 will be logged then 10 will be added.

So basically it does log the 0 before it runs the + 10. I seeeeeee. Thanks!

Linas Mackonis
Linas Mackonis
7,071 Points

Jennifer, first loop starts from 0. Since you wrote j <= 100, it will run 11 times, because it will run until it will be equal to 100. If you want it to run 10 times, remove '=' sign. Also, remove ';' from the last argument 'j += 10;' .

It should look like this:

for (var j = 0; j<100; j += 10) {
 console.log(j); 
}