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

Val Handy
Val Handy
2,395 Points

Need help with for loop code challenge.

The 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.

Here is my code. Did i miss something? Thanks for your help!!

var num; for (i = 4; i <= 156; i +=1) { num = i + β€œ ”; } console.log(num);

script.js
var num;
for (i = 4; i <= 156; i ++1) {
num = i + β€œnumber”;
}
console.log(num);
alex gwartney
alex gwartney
8,849 Points

Ok so i did the challenge and here is what i did to make it work

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

There is a few things on your code that is causing it to crash. The main thing is you have i that increments plus one but you also have 1 after the two ++ which is not needed.

And the other thing is for what they are wanting they just want you to log to the console in the for loop it self.Hope this helps if you have further questions let me know.

3 Answers

Franciscus Agnew
Franciscus Agnew
23,097 Points

Repost of my original comment...

Hello Val,

One of the problems with your code is that you have created more variables than is necessary for this program. It is important that we keep our programs as simple as possible.

So we can take your code and do this:

var  num;
for(num = 4; num <= 156; num++) {
    console.log("Number " + num);
}

You could also have written it this way:

for(var num = 4; num <= 156; num++) {
    console.log("Number " + num);
}

Do you see how that fixes your problem and where your errors were?

Cheers

Franciscus

Hi Val,

Here's a link to a recent forum question that may help you out. Let us know if it doesn't work :) https://teamtreehouse.com/forum/create-a-for-loop

Happy learning! Tracy

Val Handy
Val Handy
2,395 Points

Thanks guys!!! I will try your suggestions. I think I have a tendency to over think what is being asked. Before I got to this code, I had more than one variable applied. :) So, really appreciate you taking the time to explain!

Franciscus Agnew
Franciscus Agnew
23,097 Points

Hello Val,

Please use the ticker next to the submitted answers to confirm that they helped you in solving your problem. It will effectively credit give credit to each user for their solution that was provided.

Thanks,

Franciscus.