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

Cesare Parmiggiani
Cesare Parmiggiani
8,017 Points

Don't understand.....

I don't understand how to console.log numbers from 4 to 156.....

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

3 Answers

Jake Lundberg
Jake Lundberg
13,965 Points

Here is how I solved it:

for(var i = 0; i < 157; i++) {
   console.log( i );
}
Cesare Parmiggiani
Cesare Parmiggiani
8,017 Points

Thank you jake!

Still it seems not working... and i don't understand the i++... I got a lot to learn.

The error report is this:

You need to log out EVERY number from 4 to 156 to the console. Your loop calls the console.log() method 157 times, from 0 to 156.

Jake Lundberg
Jake Lundberg
13,965 Points

oh haha, sorry, I mistyped my code:

for(var i = 4; i < 157; i++) {
   console.log( i );
}

and i++ is another way of writing i += 1

Because incrementing by 1 is so common in programming, a shorthand was created, which is following a variable by two plus signs, as I did. This just adds one to the variable, the exact same way that i += 1 does.

Jake Lundberg
Jake Lundberg
13,965 Points

You are very close.

In your code you are actually printing numbers 4 - 157 because you are using "greater than or equal to" instead of "greater than"

you would either need:

i <= 156
or
1 < 157

also, unless you are being told to do otherwise, I would just use: console.log( i ).

Cesare Parmiggiani
Cesare Parmiggiani
8,017 Points

Ah yes! Thank you!

Still it does not work, cause i should print all the numbers between 4 and 156 and i don't understand how...

Cesare

Cesare Parmiggiani
Cesare Parmiggiani
8,017 Points

Thank yo Jake!

Now everything works, and I'm glad to even learn something new!

Thank's a lot!