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.