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

Dinasha Rollins
Dinasha Rollins
1,464 Points

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);

2 Answers

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 49,925 Points

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
Dinasha Rollins
Dinasha Rollins
1,464 Points

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

Dinasha Rollins
Dinasha Rollins
1,464 Points

thank you I for helping me figure out the problem

Gabriel Pierce-Lackey
MOD
Gabriel Pierce-Lackey
Treehouse Moderator 10,992 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)
}