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

not sure on how to tell the console to log the numbers 4 to 156

need help logging the numbers 4 to 156 to the console.

script.js
for ( ) {
  console.log();
}

2 Answers

Steven Parker
Steven Parker
229,783 Points

A "for" loop has 3 clauses inside the parentheses, an initialization, a conditional, and a final expression (or "increment").

The initialization should declare a variable and set it to the starting value.
The condition should test the variable to see if it has reached the last desired value.
And the increment should change the value between loop iterations.

The clauses are separated by semicolons.

Then, you can pass the loop variable as the argument to the log call.

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

this is what I've added to the code but I'am getting a syntax error.

Steven Parker
Steven Parker
229,783 Points

You're pretty close:

  • the "for" loop code is good but the semicolons only go between the clauses, there won't be one at the end
  • the argument to be logged is simply counter, you don't want to replicate the loop clauses there