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?

for (let counter = 3; counter >= 0; 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!

I did

for (let counter = 3; counter > -1; counter--){

It works, but the answer is marked as wrong…

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…

In this excercise, the answer is this code:

for (let counter = 3; counter >= 0; counter--) {

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

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

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?


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*/



-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!

1 Like

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

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