FAQ: Loops - Looping in Reverse

This community-built FAQ covers the “Looping in Reverse” exercise from the lesson “Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Looping in Reverse

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 (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 (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why is it not possible, in this exercise, to set counter = 0 ? Why must it be >= 0?

1 Like

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.

should the >= not be <=?

Am I being stupid??

1 Like

What does the for-loop do with the code you put there? (Seems like you’ve proved to yourself it doesn’t do what you expect. Time to look it up!)

Ah I was being stupid.

Don’t mind me!

1 Like

I did

for (let counter = 3; counter > -1; counter--){
  console.log(counter);
}

It works, but the answer is marked as wrong…

4 Likes

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…

3 Likes

In this excercise, the answer is this code:

for (let counter = 3; counter >= 0; counter--) {
  console.log(counter);
}

But isn’t the stopping condition true from the beginning in this loop? (3 >= 0). How does this loop even work?

2 Likes

If somebody wants to break (lag) program - they should try tis code:
:

1 Like

sorry, i found my answer in the previous conversations.

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?

1 Like

Consider:

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.

Hopefully this helps!

8 Likes

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:

  1. Counter starts at 3
  2. Check if counter is more than or equal to 0. If it IS, then:
  3. 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--.

1 Like

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--) {
  //Do something
}
1 Like

I guess it is the syntax that makes it a little confusing! I feel like it’s one of those things where I have to accept “that’s just how it is”… :sweat_smile:

Thanks so much for your patience!

1 Like

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?

Hello, @system4942363917.

Welcome to the forum.

In some environments, not using var, let or const will throw an error:
image

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}`);

Output:

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.

3 Likes

The exercise clearly mentions that use >= operator

1 Like

I’m still a beginner but i think the steps are done in a different order.

  1. Counter = 3…
  2. Check if counter is greater or equal to 0. Answer is yes.
  3. Print 3 to the console.
  4. 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 :slight_smile:

“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.

1 Like