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

Justin Estrada
Justin Estrada
34,995 Points

I guess I don't understand this code challenge because I obviously outputted 4-154

Challenge: Create a for loop that logs the numbers 4 to 156 to the console. To log a value to the console use the console.log( ) method.

My code, script.js: for (var num = 4; num <= 154; num++){ console.log(num); }

The error log that pops up: Create a for loop that logs the numbers 4 to 156 to the console. To log a value to the console use the console.log( ) method.

script.js
for (var num = 4; num <= 154; num++){
console.log(num);
}

14 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Justin;

You are extremely close. The challenge wants the numbers from 4 to 156. It looks like you are outputting the numbers from 4 to 154.

Ken

Justin Estrada
Justin Estrada
34,995 Points

Oh haha Ken you're awesome and have ninja vision, thanks.

Micole Noddle
Micole Noddle
5,797 Points

Would you mind explaining why we use the num++ in our code? It wasn't discussed in the video and I don't understand the challenge answer as a result. Thanks in advance!

Micole Noddle, the num++ is the same as num = num + 1; It is an incrementer. It is being used in this code in order to increase the value of num each time the loop executes. That is why the output will count up. If num++ was not present, then the loop would only every output 4 or what ever value num is initially set to.

Additionally, if we did not increment the value of num, the condition num <= 156 would never be met. This would result in an infinite loop and it would crash the browser.

I hope that answers your question.

Ken Alger
Ken Alger
Treehouse Teacher

Micole;

A for loop allows one to iterate over a range of values. The basic syntax is:

for (initialization; termination;
     increment) {
    statement(s)
}

In this code example, num++ increments the variable num each time through the loop. The ++ is a postfix operator which passes the current value of num to the function and then increments it. It is a shorthand way of doing num = num + 1;

Happy coding,

Ken

Micole Noddle
Micole Noddle
5,797 Points

Thank you Matthew and Ken!

chello yana
chello yana
8,924 Points

var num=" " ; for(var i=4;i<=156; i++) { num += ' ' + i + ' ' ; } console.log(num); same issue here.i am able to print this in console 4to 156 .but its showing incorrect in in th course challenge.

Sir please help me.i am unable to move forward because of this.

Shaun Wong
Shaun Wong
14,489 Points

The method taught in the video, For those who want to do it this way.

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

Unless, you must made a typo when asking the question, the Challenge is to print out 4 to 156 and you are printing out 4 to 154 in your for loop :

for (var num = 4; num <= 154; num++)

should be

for(var num = 4; num <= 156; num++)

Just a simple mistype.

If you just made a typo in posting your question then feel free to disregard this answer.

Justin Estrada
Justin Estrada
34,995 Points

Thanks Matt, I just had momentary dyslexia, and saw 154 and wrote 154 instead of 156. Thanks

Yeah, I have done that plenty of times on the challenges.

Ken Alger
Ken Alger
Treehouse Teacher

Challenges... sample code... project code... real life... All part of learning and being a developer, right? My family looks at me like I'm nuts when I can't see the difference between a 5 and an 8 in code.

Pleased it all worked out, peer code review can be very helpful.

Happy coding,

Ken

So it's a trick question for beginners. Are there any resources/cheat sheet with all the options?

Justin Estrada
Justin Estrada
34,995 Points

No I'm pretty sure, any syntax will work.

I was having issues with this as well. Out of curiosity, how is a beginner suppose to know to add the num++ instead of what's taught in the video? I'm running into this issue with all the challenges in this course and it's incredibly frustrating. Is there a resource that I'm missing? Thanks in advance.

Justin Estrada
Justin Estrada
34,995 Points

num ++; is the same as: num += 1; num = num + 1;

I just knew this from writing other programming languages.

You can use num += 1 as well, it will work the same...

Juan Ignacio Lambardi
Juan Ignacio Lambardi
3,943 Points

Yes Justin Estrada, TreeHouse is great. The challenge runs with this: for (var i = 4; i <= 156; i++ ) { console.log(i); }

Correct Juan ,thank you

Jodie Whipple
Jodie Whipple
11,321 Points

I feel like I have the same code but it's not resolving. What am I doing wrong?

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

Justin Estrada
Justin Estrada
34,995 Points

Hey jodie, get rid of the semi-colon in your for loop control variable increment. like this: for ( var i = 4; i <= 156; i += 1)

Jodie Whipple
Jodie Whipple
11,321 Points

Thanks a lot Justin. Those little one character errors are killing me, but I am learning from them.

Muzafar Haq
Muzafar Haq
14,272 Points

I find for loops to be more logical, and as compared to while/do-while loops. Although Dave McFarland mentioned that for loops are a bit tricky in his intro to for loops video ~

Justin Estrada
Justin Estrada
34,995 Points

To me, for loops are the most powerful because of the loop control variable. It holds the variable initialization, the comparison and the increment.

for ( variable initialization; comparison; increment;){}

Sobin Sebastian
Sobin Sebastian
5,348 Points

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

for (i = 4; i < 157; i++) { console.log(i); }

oscar arbizu
oscar arbizu
9,180 Points

Can someone explain the difference between inputting i +=i and i++ as the increments.

Cynthia Norwood
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Cynthia Norwood
Front End Web Development Techdegree Graduate 15,214 Points

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

Both work. They did not go over using the increment operator in this course though so I believe they want us to use the first one above and not the second. The ++ does this "Unary operator. Adds one to its operand. If used as a prefix operator (++x), returns the value of its operand after adding one; if used as a postfix operator (x++), returns the value of its operand before adding one." from the MDN
They also did not mention in this course leaving out the word var, so it looks like i = 4 works just fine without the var in front of it.


Marcus Smith above, you put the { before console.log and not after that's why it did not work. Your answer: for ( var i = 4; i <=156; i += 1 ) {

} console.log ( i ); just move that final curly brace to after the console.log (i); }

why doesn't my code work?

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

marcus smith
marcus smith
2,576 Points

Mine isn't working either ..... i am beginning to feel that this was a waste of time and money.....

for ( var i = 4; i <=156; i += 1 ) {

} console.log ( i );

Justin Estrada
Justin Estrada
34,995 Points

That's a horrible outlook man, I literally have my career job today, because of Treehouse. No bullshit.

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

for (initialize; comparison; increment ) { /* output increment */ }