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

I'm just not getting it.

I keep having a problem with the objectives in the Java(script) courses. I am so confused. With my code here, I am getting a GIANT error message that says my console.log is only being called once. Yet in the same error message, it is literally printing out every single number from 4 - 156. I have tried googling to figure it out on my own, it's just not happening. I really seem to struggle with these Java(script) classes. I feel like I am being taught 3+3 in the video and it makes complete sense, and then the objective asks me to do something like calculus and I just can't make the leap. It takes me absolutely forever to figure what is really seems to be only minute differences.

script.js
var html = '';

for ( var i = 4; i <= 156; i += 1 ) {
  html += "<div>" + i + "</div>";

}

console.log(html);

4 Answers

Steven Parker
Steven Parker
231,007 Points

First, let's be clear on what language this is. You mentioned "Java" twice, but this is JavaScript. I know they are somewhat similar both in name and appearance, and Treehouse has courses for both of them, but they are distinctly different languages.

Now what you did was actually kind of clever. Inside your loop, you create a single string that contains all the numbers, each inside an HTML DIV element. Your single console.log then prints out everything. If you had left off the DIV tags and replaced one with a space or newline, you would have literally satisfied the challenge! But as you noticed, the challenge is expecting you to call console.log() multiple times and not just once with all the numbers.

So, as others have suggested, calling console.log() inside the loop is what the challenge is looking for. And you only need the numbers, you don't need to construct any HTML or use an extra variable other than the loop counter:

script.js
for (i = 4; i <= 156; i += 1) {
  console.log(i);
}
Mladen Ignjatovic
Mladen Ignjatovic
13,602 Points

You must write it like this:

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

Hey Cynthia,

Try placing your console.log() function inside your for loop. Hopefully this helps.

Let us know if this still isn't working for you!

I really have to thank you all so very much. I've been trying to follow along with the videos and up to this point, I've really had to kind of piece together answers that I've searched for. Unable to really build it on my own. But with your help, I've sort of had an epiphany. It's really helped me figure out how to piece everything together on my own without help. It has really helped me make the 3+3 to calculus leap so to speak. Where he shows me an idea in the video, and I can transfer my knowledge to a similar, but not exactly the same line of code.

Thank you all again!!!