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

Sergey Filimonov
Sergey Filimonov
5,878 Points

For loops won't work

Here is the task: 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.

I've written the loop but it doesn't work :(

script.js
for ( var i = 1; i > 4 && i < 156; i += 1 ) {
  return i;
}
console.log(i);

2 Answers

Adrian Patrascu
Adrian Patrascu
15,279 Points

Hi Sergey,

Your loop will not work because the condition in the for will be false for the first 3 numbers. I would recommend initializing i = 4 from the beginning when you set var i=1 and then comparing with 156.

So for (var i=4; i <156; i +=1) { return i; }

Thank you, Adrian

Sergey Filimonov
Sergey Filimonov
5,878 Points

Thanks Adrian! Now I got it!

for ( var i = 4; i <= 156; i += 1 ) {
  console.log(i);
}
Sergey Filimonov
Sergey Filimonov
5,878 Points

Hm it's not showing it correctly for some reason. Here is the correct code http://take.ms/spsjJn