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

I did exactly like the example but I used 4 and 156 but its not working and i do not understand why

the wording of the questions are confusing please break this down better because im frustrated and confused

script.js
var numbers = '';

for ( var i = 1; 4 <= 156; i += 1 ) {
     numbers += '<div>' + i + '</div>';
}
console.log(numbers);

Can you post your updated code?

2 Answers

Some hints:

  • Since you are counting from 4 initialize i = 4
  • The condition 4 <= 156 is always true so you have created an infinite loop. Use a value that increments instead
  • You are logging to the console so won't need <div>. You can eliminate that and related code by just logging i inside your loop instead

I did everything you told me to so it still does not work

thank you I for helping me figure out the problem

Gabriel Plackey
Gabriel Plackey
11,064 Points

If you are still struggling with this, here is the correct answer. Kris was pointing you in the right direction without giving it to, but sometimes it can help to see the actual code.

We want i to equal four because we are starting at 4. Then you aren't checking 4 to be equal to or less than 156, that will never be true so it's running infinitely. We need to check i the variable were using as an index and increasing each time to be equal to or less than 156 to continue the loop or not. Then we just want to console log i, to console log each number in between the two.

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