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

Laura Pesce
seal-mask
.a{fill-rule:evenodd;}techdegree
Laura Pesce
Full Stack JavaScript Techdegree Student 7,875 Points

Problem with solving Syntax error in javascript

I need to create a for loop that logs the numbers 4 to 156 to the console, using the the console.log( ) method. I wrote this but it doesn't seem to work. What am I doing wrong?

My code:

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

script.js
 for ( var i; 4 < i < 156 ) {
  console.log (i);
 }
Charles Febryanto
Charles Febryanto
13,085 Points

you have to set the variable value first. i variable have no value so it will result in error for 4 < i condition, also your code don't have any increment or decrement for each loop

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

2 Answers

Sam Baines
Sam Baines
4,315 Points

Hi Laura - you need to set the initial 'i' variable in the first part of the loop conditional, then make sure that the variable 'i' is set to <= 156 (the point at which the loop stops running) and finally make sure you add the increment for the loop in the third part - i++ means the same as increase the variable i by 1 each time the loop runs.

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