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

Aaron HARPT
Aaron HARPT
19,845 Points

code challenge problem

I am having trouble with the following code challenge.

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 j = "";
for (var i = 4; i == 156; i = i + 1) {
j = j + i;
}
     console.log(i);

3 Answers

Giorgi Gelashvili
Giorgi Gelashvili
1,504 Points

Hello Aaron,

Why not do:

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

Just move console.log inside loop.

Hi,

You problem is the for loop:

YOUR CODE:

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

Should be:

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

If you want to log all the numbers then the console.log needs to be in the loop.

And quick explain of the FOR loop:

for ("var i = 4"; "i <= 156"; "i += 1") { "sample_expressions" }

  1. "var i = 4" -> this is the first part of the loop and you use it to declare and initialise a counter for the loop
  2. "i < 156" -> this is the conditional statement which checks the boolean value of the expression - in the first run i = 0 and this evaluates to True cos 4 is less than 156
  3. then the body of the loop is executed "sample-expressions" -> the value of i in sent to the console output
  4. "i += 1" -> the counter is incremented (or decremented: depends on what you write )

Hope this clarifies a bit.

Jeff Jacobson-Swartfager
Jeff Jacobson-Swartfager
15,419 Points

Giorgi and Nejc both have great answers.

However, you can make it a little simpler still by just cutting out j (which isn't necessary for this challenge).

I've broken up the code into multiple lines so I can point out some things that are different than yours.

for ( var i=4; // This starts off the for loop at the number 4.
      i<=156;  // Instead of testing for the condition of i==156 
               // (which only runs when i is equal to 156), I'm 
               // testing for i<=156 (which runs as long as
               // i is less than or equal to 156).
      i++ ) {  // This is just Javascript's incremental iterator shorthand
        console.log(i); // Logs i to the console for each pass through the loop.
}

And there you go! The same behavior with less code!

Here's a version without all of the comments:

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