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

Peng-Wen Lai
PLUS
Peng-Wen Lai
Courses Plus Student 595 Points

for loop

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.

script.js
var i;
for( i=0; i <+ 156; i += 4) {
  console.log(i)
  document.write(i);
}

I can't really understand the question, what does it want me to do ??

2 Answers

Hi Peng-Wen,

The Challenge wants you to console.log() some numbers. Specifically, it wants you to log the numbers 4 through 156, inclusive. You've almost done it in your for-loop above... just a few syntax and logic errors:

  1. "<+" is not valid syntax for anything. What you want is the less-than-or-equal-to operator, which looks like this: "<="
  2. "i += 4" is telling your for-loop to essentially count by 4. Since you're starting at i=0, that means the numbers you ultimately log will be: 0, 4, 8, 12, 16 etc. Obviously, this isn't what the Challenge wants you to do. The Challenge wants you to start at 4 and count by 1 until you reach 156. Let me know if you need further hints on this.
  3. Finally, there is no need for document.write(). The Challenge just wants you to console.log your numbers.
Alex Thomas
Alex Thomas
4,247 Points

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

I think this is correct, no? I'm still not sure where, when, or why I would use this. I mean I think I know why, to repeat an aspect of code without writing it 156 times. Ok, I just answered my own question. Ain't coding fun and challenging?!?!!?!?!?! What I don't understand is why no semicolon needed after parenthesis (spelling). And why is it called a for loop?