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

Assaf Almog
Assaf Almog
3,174 Points

No need of semi-colon; at the end of inside the loop ?

in the do.. while loops lesson 10. the example code was -

var html = ' ' ; for ( var i =1; i <= 10000; i += 1 ) { html += '<div>' + i + '</div>'; } document.write(html);

is there no need of semi-colon after the (i += 1) ?

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

In short, no. A for loop should look like this:

for(i = 0; i < someNumber; i++) {
  doSomeCode();
}

But, I'd suggest you go back and look at the example code again because what you've posted in the question won't run. A for loop always has three parts. The first part keeps track of the counter (in this case i). The second keeps track of how many times it should go through the loop. And the third part increments (or decrements) the counter.

Assaf Almog
Assaf Almog
3,174 Points

Thanks Jennifer, that's just what i thought. it was just the third part of the loop that doesn't END with a semi-colon; that bothered me :)

BTW: how did you post an html comment ?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

There's a nice little markdown cheatsheet link here on the forums that will explain how to post your code :) As a side note: that bothered me too when I was first learning, but it'll be second nature in no time!