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 trialMichael Pashkov
22,024 PointsBummer! You need to log out EVERY number from 4 to 156 to the console. Your loop calls the console.log() method 39 times
What I am doing wrong?
Create a for loop that logs the numbers 4 to 156 to the console. To log a value to the console use the console.log( ) method.
var number = 4;
for ( var i = 4; i <= 156; i += 4 ) { console.log( number ); number += 1; }
var number = 4;
for ( var i = 4; i <= 156; i += 4 ) {
console.log( number );
number += 1;
}
2 Answers
Prsicilla Anesu Muzerengwa
12,577 PointsHi Michael Variable I is being used as the counter so it is created inside the loop and set its starting value to 4, your condition is correct i <= 156, then you need to increment the value stored inside i by one not 4. Like this i+=1 The increment is done each time the loop runs. So the sode looks like:
for( var i = 4; i <= 156; i+=1){
console.log(i);
}
Michael Pashkov
22,024 Pointsthank you :)
Jennifer Nordell
Treehouse TeacherHi there! I feel like you're doing pretty well, but you've set up a number
variable outside your for loop. Inside the for
loop you're then echoing out number
and increasing it by 1. The number
variable is not needed at all. You're meant to be logging out the value of i
. The value of i
is increasing every iteration by four instead of by one.
Give it another shot with these hints in mind, but let me know if you're still stuck!
Michael Pashkov
22,024 PointsThank you for your help.
Michael Pashkov
22,024 PointsMichael Pashkov
22,024 PointsI did in Workspace. Sorry, I copied and pasted wrong. What stuff I did.
var number = 4;
for ( var i = 4; i <= 156; i += 4 ) { console.log( number ); number += 4; }