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

Cody Norman
seal-mask
.a{fill-rule:evenodd;}techdegree
Cody Norman
Full Stack JavaScript Techdegree Student 3,421 Points

"for" challenge

I'm not sure where I'm going wrong here, best I can tell I'm supposed to log to the console individual numbers from 4 to 156, however with console.log outside the loop I get the message that I didn't do it right since it only logged once (number 4-156) and that I need to log EVERY number. But put console.log in the loop and I get "The console should write out 4 the first time in the loop and 156 the last time through the loop." I don't recall learning how to log individual numbers in a loop. Can I please get some help here?

script.js
var count = '';

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

2 Answers

Tomas Schaffer
Tomas Schaffer
11,606 Points

Hi Cody, The code below works. But let me explain your mistake. First You don't need 2 variables for this. Your variable "count" is no needed and even if it is. You can create empty variable like this: "var count;" No empty string is needed. You have to store number 4 which should be in console first. After you set condition how long should loop executing. Last you adding +1 to variable i but after when was executed. If you write ++i or i++ it is different. In my example i am adding 1 after code was executed in other case ++i you add +1 before and first result would be 5.

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

Tomas Schaffer
Tomas Schaffer
11,606 Points

And i Forgot i +=1 is same as i++ or as i = i +1