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

Edward Kiefer
Edward Kiefer
2,077 Points

Stuck on this ' for loop ' code challenge...

This is the question: 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.

This is as far as I have got with it. Can anyone point me in the direction of what I am getting wrong. Many thanks for your time.

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

since it tell you start from 4, make the variable i has the value of 4, then in conditional statement put i <= 156 and the increment shorthand i++ since it tell you to just increment it by 1 for each loop. You don't need the parentheses for the condition

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

1 Answer

Sam Baines
Sam Baines
4,315 Points

Hi Edward,

You have over complicated the basics of the loop - start with variable 'i' as a value of 4 and then have the conditional for <=156 and the increment is fine - the code should read:

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

Also double check all your brackets in future as you seemed to have several odd pairs in your first code attempt.