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

Stuck on for loops

I keep trying different versions of this and I don't understand.. Aren't I doing just what he did in the video? Do I have to define a variable at the beginning, like I did here with 'print'? Why can't I just say console.log(i)? I understand what's in the first parentheses of the for loop, but what's in the curly brackets makes no sense to me.

script.js
var print = "";
for (var i = 4; i <= 156; i += 4;) {
  print += i;
}

console.log(print);

In addition to the other answers, I want to add this to help you understand what is happening with your for loop. You have already defined a variable i. You can use console.log(i) as in the other answers, but you have to consider the scope of that statement.

The variable print is completely accessible to all of your code, but "i" is not. "i" is only accessible inside your for loop, It looks like you are getting stuck because console.log() is outside your loop.

2 Answers

The question asks you to print to the browsers console all numbers between 4 and 156.

You wasn't far away, but basically you were adding 4 to i for each iteration, and then attempting to increment the "print" variable with i each time. What you actually needed to do was simplify this to increment i by 1 each time, and within the for loop, print to the console using the console.log() function as below.

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

Your answer was also printing the total of print instead of each interation.

Thank you, it's kind of starting to make sense, jeez this is confusing! Thanks. :)

Michael Liendo
Michael Liendo
15,326 Points

@micheleg You're intuition is right :)

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

would pass the challenge (tested). The difference between the solution above and the one you had in your question is that in yours, all of the numbers are converted to a string (with no spaces), and then that entire String of numbers is printed once to the console. ie: ("48121620....") --since you're adding 4 to each value,as opposed to the way I described, where the value of each number printed to the console is a Number. Additionally, when printed, each value will be on its own line ie:

4
5
6
 and so on ...

Hope that helps :)

Thank you for your help, it's still a little hazy but I'll keep reading this and going over the lesson until I get it, thank you!