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 Create a for Loop

james mchugh
james mchugh
6,234 Points

I can't get his one

I tried to follow the instructors example and it is telling me syntax error. I really don't understand why.

script.js
var html = ' ';
for (var i = 4; i <= 156; i += 1;) {
  html += '<div>' + i + '</div>';
}
console.log(html);

1 Answer

Manish Giri
Manish Giri
16,266 Points

First, you have a syntax error. You don't put the ; after the last condition in a for loop, so the syntax should be -

for(var i = 0; i < 100; i += 1)

Last, you just need to console.log the number i in the loop. You're instead adding a <div> each time the loop runs, and then logging the result, which is why it won't work.

Just console.log(i); inside the loop.

james mchugh
james mchugh
6,234 Points

I tried that and get this message. Bummer! You need to log out EVERY number from 4 to 156 to the console. Your loop calls the console.log() method 152 times. This is what I ran. for(var i = 4; i < 156; i += 1) console.log(i);

Manish Giri
Manish Giri
16,266 Points

Show your code then.

james mchugh
james mchugh
6,234 Points

This is what I ran. for(var i = 4; i < 156; i += 1) console.log(i);

Manish Giri
Manish Giri
16,266 Points

Your loop does not include 156, which is what you had initially - i <= 156.

james mchugh
james mchugh
6,234 Points

Thanks Manish, Yeah that last time I ran it I forgot the = after the <. That worked thank you