There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
for (let counter = 3; counter >= 0; counter–){
console.log(counter)
};
works, but for me the loop should be stopping after one (or possibly no) iteration because 2 (or 3) >= 0.
Hello, just a beginner myself, but I think I can help.
“a stopping condition is the condition that the iterator variable is evaluated against— if the condition evaluates to true the code block will run, and if it evaluates to false the code will stop.”
If you set your stopping condition to counter === 0, then the condition with evaluate to false right away and your code will never run…
I’m trying to understand logically why the console.log stops at 0.
If the condition is to subtract one from counter and print to console while counter is more than or equal to 0, shouldn’t the last number printed to the console log be -1?
for (let counter = 3; counter >= 0; counter--) { //the condition 'counter >= 0' evaluates before the console.log(counter) statement is executed
console.log(counter); /*when counter gets to -1 the condition 'counter >=0' evaluates to false so this statement is not executed*/
}
Output:
3
2
1
0
-1 Doesn’t get logged to the console because -1 >= 0 evaluates to false, and control flow is passed on to whatever comes after the for loop.
This statement has 3 parts:
for (let counter = 3; counter >= 0; counter--)
First:let counter = 3 This statement only executes once.
Second:counter >= 0 This condition is checked before anything inside of the code block is executed. This check occurs immediately after the variable count is either declared, incremented or decremented.
Third:counter-- This statement is executed following the execution of code inside the code block { }, and before counter >= 0 is checked again.
Thanks for your detailed response! I’m still a bit confused though.
Third:counter-- This statement is executed following the execution of code inside the code block { } , and before counter >= 0 is checked again.
I think this is the part I don’t understand. Because counter-- comes before console.log(counter) in the code. The way I interpret the code is:
Counter starts at 3
Check if counter is more than or equal to 0. If it IS, then:
Deduct one from counter and print the new value
Also meaning that the console.log would start at 2 as well. Sorry if there’s something obvious I’m really not getting but my main question is why console.log happens before the counter--.
Many modern programming languages use a sort of shorthand which is more or less what the JavaScript for loop is. In VISUAL BASIC the logic is more evident:
For counter = 3 To 0 Step -1 //the step parameter tells Next the increment amount
//Do something
Next counter //counter incremented by -1
The same order of events occurs in JavaScript, but the syntax is different:
for ( let counter = 3; counter >= 0; counter- -) …
and
for ( counter = 3; counter >= 0; counter- -)…both iterations give the same answer after logging. I have seen in certain languages “let” is left out. Can we leave it out here too?
In some environments, not using var, let or const will throw an error:
There is also the issue of scope. Variables declared without one of the aforementioned key words will have global scope. In simple, small projects, that may not be an issue, but in larger projects giving a variable scope beyond it’s needs can become problematic.
The first example shows how a variable nested inside a for loop that is nested inside a function can still be accessed outside of the function when declared without a key word versus the second example which throws an error when trying to access the block scoped variable:
//First Example
const countDown = num => {
for (count = num; count >= 0; count--){
console.log(count);
}
}
countDown(3);
console.log(`Even though it is no longer useful, we still have access to the value of count: ${count}\n`);
//Second Example
const countDown2 = num => {
for (let count2 = num; count2 >= 0; count2--){
console.log(count2);
}
}
countDown2(3);
console.log(`This won't be printed because count was limited to block scope: ${count2}`);
In this case the error is preferred. We don’t want to pollute the namespace with useless global variables. The count2 variable no longer exists once the for loop is finished.
I’m still a beginner but i think the steps are done in a different order.
Counter = 3…
Check if counter is greater or equal to 0. Answer is yes.
Print 3 to the console.
Counter–
So finally the result is : 3 2 1 0;
I think you’re done with loops since the time you posted it, but it can help other people who read this thread like me. I was confusing at the beginning too
“a stopping condition is the condition that the iterator variable is evaluated against— if the condition evaluates to true the code block will run, and if it evaluates to false the code will stop.”
So as long as the number is >= 0, the code will run. If you set it to <= 0, the code will immediately evaluate false and never run.