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 trialCesare Parmiggiani
8,017 PointsDon't understand.....
I don't understand how to console.log numbers from 4 to 156.....
var html='';
for ( var i = 4; i <= 157; i += 1 ) {
html += i ;
console.log(html);
}
3 Answers
Jake Lundberg
13,965 PointsHere is how I solved it:
for(var i = 0; i < 157; i++) {
console.log( i );
}
Jake Lundberg
13,965 PointsYou 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
8,017 PointsAh 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
8,017 PointsThank yo Jake!
Now everything works, and I'm glad to even learn something new!
Thank's a lot!
Cesare Parmiggiani
8,017 PointsCesare Parmiggiani
8,017 PointsThank 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
13,965 PointsJake Lundberg
13,965 Pointsoh haha, sorry, I mistyped my code:
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.