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 trialGeorge Akinian
17,615 Pointshow come the initial first time it knows to run counter = 10 then checks counter, then it proceeds to console.log?
how come the initial first time it knows to run counter = 10 then checks counter, then it proceeds to console.log("Hello World") and bypasses counter =counter- 1 ? However, the second time around it does evaluate counter=counter - 1 and comes up with a following solution which supersedes one before.
Jason Anello
Courses Plus Student 94,610 PointsHi Paul,
Are you referring to the for
loop or the while
loop in the video?
3 Answers
Phil Pickering
1,343 PointsThe for statement is used for creating a loop.
The syntax of the for statement allows you to use 3 expressions in parentheses, separated by semi-colons:
for ( [initialization]; [condition]; [final expression] ) {
// block of code
}
The first expression in the parentheses is used purely for initialization and is not part of the loop. The actual loop begins with the condition expression, and ends with the final expression.
If the condition expression evaluates to 'true', then the block of code enclosed in the curly braces is run. Then the final expression is evaluated and the current cycle of the loop is complete. The computer then returns to the condition expression and evaluates it again.
If the condition expression evaluates to 'false', then the loop is terminated and the block of code in the curly braces is not run, and the final expression is not evaluated.
Andrew VanVlack
20,155 PointsSo a while loop will continue to loop as long as what is in the brackets is true. True and false can be the boolean values of true or false, or it can be a binary number with 0 as false and 1 as true. In java script any integer above 1 is also true so when the loop starts at 10 (set by the variable 'counter') there is a true statement so it runs the loop. Every time the loop is ran the variable 'counter' decreases by 1 and runs over and over till counter gets to 0 and ends the loop.
Robert Greenlees
1,989 PointsOne clarification to your answer - the first sentence should be revised to say "So a while loop will continue to loop as long as what is in the parentheses is true."
Robert Greenlees
1,989 Points"While" looks to the condition within the "()" and then executes the action(s) within the "{}" once each time the condition within "()" is true.
Each time the loop runs the variable "counter" is decreased by 1, so the sequence looks like:
"counter" starts at ten, which means the condition is true (any integer greater than zero is considered truthy) then at the very end of the loop the counter variable is reduced by 1 and returns to the "()" to evaluate whether it needs to run again.
"counter" starts at 9, repeats the same behavior as the first loop, reduces by 1 and returns to the "()" to reevaluate.
"counter" at 8 now etc.
"counter" at 7 now etc.
"counter" at 6 now etc.
"counter" at 5 now etc.
"counter" at 4 now etc.
"counter" at 3 now etc.
"counter" at 2 now etc.
"counter" at 1 now and the actions within the "{}" are executed again, resulting in reducing "counter" to zero.
When the condition "(counter)" is evaluated it is no longer true because "counter" now is equal to zero. There is no further action to take because the content within "{}" only run when the condition is true, but now "(counter)" would evaluate to false because "counter" is no longer greater than zero - it is zero and is therefore falsey.
If you have questions based on this response please reply directly to the comment.
Ethan Lowry
Courses Plus Student 7,323 PointsEthan Lowry
Courses Plus Student 7,323 PointsHey, please post the specific code in question so we can get a better idea of what you're asking. Thanks.