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 trialAbdullahi Abdinasir Ali
9,520 PointsHow for loop work
Am getting ass kicked by js please some help
for (var log = 4; log > 152; log =+ 0){
}
console.log(log)
2 Answers
tobiastrinkler
Full Stack JavaScript Techdegree Student 16,538 PointsFirstly 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
8,887 PointsHi. Let me try to explain.
This is the code you need to run:
for (var i = 4; i < 157; i++) {
console.log(i);
}
- The for-loop is using a variable called i that is set to a number (this time its 4!).
- 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.
-
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
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!
Abdullahi Abdinasir Ali
9,520 PointsGood answer I understood but the number is about 156
Alfredo Leal
Courses Plus Student 1,676 PointsAlfredo Leal
Courses Plus Student 1,676 PointsWell I think you are going nowhere ading zero to the variable. Try adding 1 instead