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

Would this not work?

I keep getting this error "You need to log out EVERY number from 4 to 156 to the console. Your loop calls the console.log() method 152 times." this course seems to be very challenging and I am having issues knowing what I am suppose to do. I have yet to have a work space work the way it is suppose to when i follow along. Could it be from using let instead of var?

script.js
for ( let i = 1; i <= 156; i += 1) {
  if( i > 4 ) {
      console.log(i);
     }
}

2 Answers

Steven Parker
Steven Parker
231,236 Points

The test "i > 4" doesn't include 4, so that will be missing from the output.

But an even better way to do this would be to initialize "i" to 4 in the loop, and then you don't need the test at all.

what do you mean initalize "i" to 4 in the loop? thank you for your time in helping. Steven Parker

Steven Parker
Steven Parker
231,236 Points

Initialization is the first of the 3 clauses of a "for" loop:

for (let i = 4;                    <-- initialization
                i <= 156;          <-- condition
                          i += 1)  <-- increment

Oh ok, I went back and made the changes and it worked flawlessly. Steven Parker Thank you so much!