FAQ: Loops - Looping in Reverse

lol i created a neverending loop by accident, but i hit the refresh button in my browser and got out of it. my iterator was getting more and more negative with each loop because i left off the >=0 part

(remember Forest Gump, when the crowd has to tell him to stop in the end zone?)

this code worked but i still cant figure out why it wont let me proceed:


for (let counter = 3; counter < 4&& counter>=0; counter–){

console.log(counter);

1 Like

so this loop will evaluate AS LONG AS counter >= 0, … who’s on first lol

i have been on this exercise for 20 minutes, time to move on!

Ola, isso tem alguma relação com a ordem de execução? no caso da esquerda para a direita .
como a explicação diz que devemos iniciar com maior valor. nesse caso ele fixa o 3 verifica faz decremento para a condição true, tornou false entra no bloco?

I wrote the following code

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

  console.log(counter);

}

and the console output went from 0 and kept counting down. It froze the page and I saw -42 before I gave up trying to scroll. I don’t know what went wrong. I was able to delete the last curly bracket and replace it all with the error message. It still gave me the check for the lesson and apparently that is the right code. So why did it go from 0 and count down?

I just copied and pasted your code and works fine for me

The lesson 4/10 states:

When writing/changing loops, there is a chance that our stopping condition isn’t met and we get a dreaded infinite loop which essentially stops our programming from running anything else!

I tried to log different for loops to achieve this, for example:

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

With the above code, the console does not print anything and when I try to rerun the lesson-code, the console still does not print anything. Is this an example of an infinite loop?

Yes, this is an infinite loop.

1 Like

Hi, can someone clarify if my understanding is correct -
The execution of the code in the curly brackets is done at the moment the stopping condition is compared to the iterator variable. After that point the variable is updated according to the iteration statement. I was confused as to why the variable wasn’t incrementing or decrementing prior to the code being executed, given the iteration statement is written inside the for expressions, prior to the code it executes.

// The loop below loops from 0 to 3. Edit it to loop backwards from 3 to 0

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

hi, I just wonder why does stopping condition must be >= 0 instead of = 0 ?

counter >= 0 is not the condition that makes the loop stop, rather it is the condition that makes the loop iterate if it is true. If counter >= 0 is true, only then will the loop run.

counter = 3 the first time, 2 the second time, 1 the third time, and 0 the fourth time. Then counter = -1 on the fifth iteration, and it is no longer >= 0, so the loop ends.

2 Likes

so, for some reason, it won’t accept this and I don’t know why. Even though the result is identical.

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

Could it be because there is no let?

for (let i = 3; ...)

hahaha, I did forget to mention it the first time and that was my original mistake, but, for some reason, it was stubborn to be solely in their syntax. I’ve noticed that with Codecademy. Such as in C, it won’t let me use ‘using namespace’, it’s odd like that.

1 Like

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

I got it after watching the For Loops youtube video of WhatDev and following the instructions of this exercise.

Why do I have to set the stopping condition (counter >= 0) which comes after the initialization ( let counter = 3) and not ( counter > 0 ) to get the right answer.
isn’t the number 1 is bigger than 0 and when checked against the stopping condition should evaluate to true?

This permits 0 to be logged as well as 3, 2, and 1.

I understand that I am asking why isn’t one bigger than 0 so is not it run through the loop one last time?

If we want to print the zero, then, i >= 0. If we want to stop on 1, then, i > 0

Hi, I completed the task correctly but I played around with it some more and I want to understand the logic behind this.

So when I use:
for (let counter = 3; counter >= 0; counter–){
console.log(counter);
};

the console prints 3 2 1 0

But when I type:
for (let counter = 3; counter => 0; counter–){
console.log(counter);
};

the console goes in an infinite loop. Why is this?

Hey, @apss-101 welcome to the forums.

For the first loop, it outputs what it is supposed to. But for the second loop => is NOT an operator but >= is. => is used for declaring arrow functions. I don’t know what the interpreter does to get this to be an infinite loop but is definitely the result of the operator.

1 Like