Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Create a for Loop

Cody Norman
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?

var count = '';

for (var i = 4; i <= 156; i +=1) {
  count += i;

2 Answers

Tomas Schaffer
Tomas Schaffer
11,594 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,594 Points

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