Prevent Infinite Loops with a Valid Terminal Condition1

Prevent Infinite Loops with a Valid Terminal Condition1
0

#1

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


#2

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.


#3

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


#4

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


#5

sorry but I forgot that I have post for help.


#6

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…


#7

Can you tell me exactly what to ood?


#8

*do?(That I mean)…


#9

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


#10

this video may help you understand for loops


#11

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


#12

Learning to program can be difficult at times. Even if you have many years experience, trying to learn something new can still be frustrating and difficult.

What specifically are you not understanding about for loops? The syntax? How it works? If so, post some for loop code and tell us what parts you do not understand.


#13

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


#14

If it is the syntax you do not understand, then read through the for loop documentation and let us know what you don’t understand.


#15

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

Is right?


#16

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.


#17

This code above is right.