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 For Loops

not sure why my code isn't printing to page

var html=''; for (var i = 1; i <= 10; i += 1); { html += 'div' + i + '</div>';

} document.write(html);

2 Answers

Not sure what you mean by not printing on the page, but I was able to get 11 to show via document.write

  var html = '';
  for (var i = 1; i <= 10; i += 1); {
    html += '<div>' + i + '</div>';  // You forgot the < > for your opening <div>
  }
  document.write(html);

HOWEVER... I followed the same code as explained in the video and got a "Uncaught SyntaxError: Unexpected token +=

I then changed the ending condition in the for loop to i++, then it worked, as shown in the video.

var html = '';
for (var i = 0; i <= 10; i++) {
  html += '<div>' + i + '</div>';
}
document.write(html);
Adrien Contee
Adrien Contee
4,875 Points

Gena here's your code. I've went ahead and highlighted the errors...

var html='';

for (var i = 1; i <= 10; i += 1); { //There is a semicolon after your condition statement -- it's not needed.
    html += 'div' + i + '</div>'; //Also make sure you add the angle brackets (< >) to your html tags or they're just viewed as strings
} 
document.write(html);

Javascript like any other programming language is unforgiving when it comes to typos and syntax errors. Here's your code again after cleaning it up and removing the errors.

var html='';

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