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 trialJustin Estrada
34,995 PointsI 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.
for (var num = 4; num <= 154; num++){
console.log(num);
}
14 Answers
Ken Alger
Treehouse TeacherJustin;
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
Shaun Wong
14,489 PointsThe 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);
}
Matthew Krueger
18,416 PointsUnless, 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
34,995 PointsThanks Matt, I just had momentary dyslexia, and saw 154 and wrote 154 instead of 156. Thanks
Matthew Krueger
18,416 PointsYeah, I have done that plenty of times on the challenges.
Ken Alger
Treehouse TeacherChallenges... 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
Sean Dunbar
3,647 PointsSo it's a trick question for beginners. Are there any resources/cheat sheet with all the options?
Justin Estrada
34,995 PointsNo I'm pretty sure, any syntax will work.
Sean Dunbar
3,647 PointsI 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
34,995 Pointsnum ++; is the same as: num += 1; num = num + 1;
I just knew this from writing other programming languages.
mark cromer
7,840 PointsYou can use num += 1 as well, it will work the same...
Juan Ignacio Lambardi
3,943 PointsYes Justin Estrada, TreeHouse is great. The challenge runs with this: for (var i = 4; i <= 156; i++ ) { console.log(i); }
Joyce Mapuranga
1,513 PointsCorrect Juan ,thank you
Jodie Whipple
11,321 PointsI 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
34,995 PointsHey 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
11,321 PointsThanks a lot Justin. Those little one character errors are killing me, but I am learning from them.
Muzafar Haq
14,272 PointsI 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
34,995 PointsTo 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
5,348 Pointsfor (i = 4; i <= 156; i++) { console.log(i); }
Marlon Martins
615 Pointsfor (i = 4; i < 157; i++) { console.log(i); }
oscar arbizu
9,180 PointsCan someone explain the difference between inputting i +=i and i++ as the increments.
Cynthia Norwood
Front End Web Development Techdegree Graduate 15,214 Pointsfor (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); }
victoria Geiger
574 Pointswhy doesn't my code work?
for( var i = 4; i < 156; i++){ console.log(i); }
marcus smith
2,576 PointsMine 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
34,995 PointsThat'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 */ }
Justin Estrada
34,995 PointsJustin Estrada
34,995 PointsOh haha Ken you're awesome and have ninja vision, thanks.
Micole Noddle
5,797 PointsMicole Noddle
5,797 PointsWould 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!
Matthew Krueger
18,416 PointsMatthew Krueger
18,416 PointsMicole Noddle, the
num++
is the same asnum = num + 1;
It is an incrementer. It is being used in this code in order to increase the value ofnum
each time the loop executes. That is why the output will count up. Ifnum++
was not present, then the loop would only every output 4 or what ever valuenum
is initially set to.Additionally, if we did not increment the value of
num
, the conditionnum <= 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
Treehouse TeacherKen Alger
Treehouse TeacherMicole;
A for loop allows one to iterate over a range of values. The basic syntax is:
In this code example,
num++
increments the variablenum
each time through the loop. The++
is a postfix operator which passes the current value ofnum
to the function and then increments it. It is a shorthand way of doingnum = num + 1;
Happy coding,
Ken
Micole Noddle
5,797 PointsMicole Noddle
5,797 PointsThank you Matthew and Ken!
chello yana
8,924 Pointschello yana
8,924 Pointsvar 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.