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

Vardas Pavarde
Vardas Pavarde
641 Points

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( )

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.

8 Answers

In this case, you have to create a counter variable called 'i' inside the loop and set its starting value to 4, then u have to create a condition saying 'i <= 156', next you need to increment the value stored inside i by one each time the loop runs.

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

Mark my answer as the best if i managed to help u

Solomon Ray
Solomon Ray
4,021 Points

Hey Guys!

this is how i did it:

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

Amar Mohammed
Amar Mohammed
6,795 Points

That worked, thank you!!!

you need to create for loop and start with initial value i = 4 and stop when i is 156 ,and print i each time

for ( i = 4 ; i< 157; i++){
  console.log(i);
}
KARMA DRUKPA
KARMA DRUKPA
7,235 Points
for (let i = 4; i <157; i++){
  console.log(i); 
}

I Don't know why this code is not working, it should definitely work as of my best knowledge.  TreeHouse is not marking as right answer.
``

for ( let i = 5 ; i <= 100; i++ ) { console.log( i ); }

This can solve your problem. It shows that there should be an increment of (i) up to 100 times. I tried on the console of chrome developer tools.

Nasheed Rashad
Nasheed Rashad
11,243 Points

this is the only one that worked for me - thank you!

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

This is a correct answer. You can try it.

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