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

create a for loop challenge

Please help, i'm confused.. the question is asking me to create a for loop that prints out all the numbers from 4 to 156 to the console. This is my code below. Thanks!

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

script.js
for ( var i = 4; i < 156; i++ ) {
 console.log(152); 
}

3 Answers

Stephen Layton
Stephen Layton
8,643 Points

Your console.log is writing 152.

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

It should be writing the variable i that contains the count.

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

As its currently written it will only write up to 155 so you'll need to adjust your for loop to account for that.

Thanks Stephen it worked!

The variable i is what will generate the numbers between four and 156. So you will need to change the < to <= to include 156 as well and console log the i. Vew below

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

I hope this helps.

Thanks Chyno! Had to change it from 152 to 157

That works as well haha. That's the beauty of JavaScript. There are so many ways to get the same results.