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

How for loop work

Am getting ass kicked by js please some help

script.js
for (var log = 4; log > 152; log =+ 0){
}
  console.log(log)
Alfredo Leal
Alfredo Leal
Courses Plus Student 1,676 Points

Well I think you are going nowhere ading zero to the variable. Try adding 1 instead

2 Answers

Firstly your loop would only print numbers 4 to 152. Secondly you are not increamenting the log variable at the end of the for loop with one.

for(var i=4; i<=156;i++){
console.log(i)
}
Andreas Nyström
Andreas Nyström
8,887 Points

Hi. Let me try to explain.

This is the code you need to run:

for (var i = 4; i < 157; i++) {
  console.log(i);
}
  1. The for-loop is using a variable called i that is set to a number (this time its 4!).
  2. The second argument in the four-loop is for how long it should loop around. If you want it to go from 4 until AND 156 you need to tell it to "go from 4 and stop when you have printed out 156". Therefor you need to do: "i less than 157". Because if it is the same value in this example, it won't print out 156.
  3. The third argument is what you want to happen to the variable i. Here I am using "i++". Which is the same as:

    i++
    //or
    i = i+1
    //or 
    i += 1
    
  4. then you console.log(i), which has a new number every loop. But you need to have this inside your for-loop brackets, see the printed code on the top of this message.

Hope this helps, keep at it and happy coding!

Good answer I understood but the number is about 156