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

Allison Guthrie
Allison Guthrie
6,781 Points

Stuck in a for loop

Hmmm...can someone smarter than me please help me out? I feel like my error is a dumb one, but I can't seem to figure it out. I am trying to create a 'for' loop that logs the number 4 - 56 in a console.log method. In advance, thank you!

2 Answers

A for loop has 3 parts initialization, condition and final-expression. your var i = 4 is is the initialization. You condition is wrong. You needs something that going to go to true or false. so i < 100 is an example. Your final expression runs ever time the loop runs. So if you want to go from 4 to 100 you would do i++. Here is what that looks like

for(var i = 4; i < 100; i++) {
  console.log(i);
}
Allison Guthrie
Allison Guthrie
6,781 Points

thank you so much for answering...makes sense. i appreciate your time!

Aaron HARPT
Aaron HARPT
19,845 Points

Here is the code that worked for me: for (var i = 4; i < 157; i = i + 1) { console.log(i); }

Allison Guthrie
Allison Guthrie
6,781 Points

Thank you! I so appreciate this!