Prevent Infinite Loops with a Valid Terminal Condition1

Tell us what’s happening:

Please help I don’t know what to do?

Your code so far


function myFunc() {
  for (loop i = 1; i != 4; i += 2) {
    console.log("Still going!");
  }
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/debugging/prevent-infinite-loops-with-a-valid-terminal-condition

Try reading this page https://www.w3schools.com/js/js_loop_for.asp
It has more explanations about for loops and then after you read retry the challenge.

Klaudia why did you make another post instead of continuing the last one?

Try write down the value of i at each step of the loop and look at when the loop should end

1 Like

I have read this site but it is difficult for me.

sorry but I forgot that I have post for help.

ok I can do a little hand-holding here but I need your co-operation…

Can you write a for loop that counts from 0 to 10 ? If you can do that (pls post code here), I can help you understand this lesson. But if you are struggling, please go back to a previous lesson on for loops to re-try the challenge on for loops before coming back to this one…

Can you tell me exactly what to ood?

*do?(That I mean)…

function loopy() {
while (true) {
console.log(“Still going!”);
}
}With that code I have the second right.It is right?

this video may help you understand for loops

Yes I saw it.But sorry again I don’t understand because I believe it is hard.

I don’t understand how can I write the code the syntax.

I read it and I write this code:
function myFunc() {
for (let i = 1; i <= 4; i += 2) {
console.log(“Still going!”);
}
}

Is right?

what you wrote above is exactly the same as saying:

let i=1;
while (i <= 4) {
  console.log("Still going!");
  i+=2;
}

So i will start at 1
compare 1 to 4, is it <=4 ? yes
log the message to the console “Still going!”
increment 2 to i so i becomes 1+2 = 3

i is 3 , is it <=4 ? yes
log the message to the console “Still going!”
increment 2 to i so i becomes 3+2 = 5

i is 5, is it <=4 ? no , therefore do not execute the code in the loop

So as you can see, what you wrote definitely is not an infinite loop like the one you started with (i != 4)

Edit: and yes your new code should pass the challenge.

This code above is right.

1 Like