JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Create a for Loop

Gedeon Bashimbe
Gedeon Bashimbe
1,023 Points

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
172,046 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.

Gedeon Bashimbe
Gedeon Bashimbe
1,023 Points

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
172,046 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