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

Abdullah Al Faruk
PLUS
Abdullah Al Faruk
Courses Plus Student 3,270 Points

I don't understand, what is asking for?

Can anyone solve this ?

script.js
for( var a = 1; a < 157; a += 1 ){

    console.log( a );

}

3 Answers

andren
andren
28,558 Points

The instructions specify that it wants the numbers 4 to 156 printed, not 1 to 156 which is what your code produces.

To change the starting number of the loop you just need to change what a is assigned at the start, like this:

for( var a = 4; a < 157; a += 1 ){ // Changed a from 1 to 4
    console.log( a );
}

Then the code will work.

? You're really close,

the challenge asks you to print to the console all the numbers between 4 to 156... So just change var a = 1 to var a = 4

Ari Misha
Ari Misha
19,323 Points

Hiya Abdullah! This is the syntax for "for" loop:

for(initialization; boolean expression; step ){
   //the magic happens here
}
  • Initialization: This statement includes declaration of an initial variable, like in other words , it pretty much starts your loop and this is the initial point for the loop.

  • Boolean expression: This part of "for" loop is a boolean expression. When "for" loop gets to this part, it checks the expression for "truthiness" , and if its true, it proceeds, otherwise it breaks the loop. And thats how your loop knows when to stop or break, whenever this statement turns to "falsiness".

  • step: This part of "for" loop requires steps like how much steps you want your "for" loop to take in order to execute the code in the body of the loop.

So yeah "for" loop execute these three steps in every step of the execution. Here is the code for your challenge , evaluate it and try to understand the logic behind it:

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