Javascript debugging confusion

Curious:

if != implys that the initialized is unequal to 4 and it creates an infinite loop, then why does >=4 (init greater than four) resolute to “undefined” and does not create any loop, whereas <=4 (init 1, true statement) creates an ending loop… shouldn’t any False condition create an infinite loop?


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

console.log(myFunc());
  **Your browser information:**

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

Challenge: Prevent Infinite Loops with a Valid Terminal Condition

Link to the challenge:

!= 4 will always be false. <= 4 must eventually be true.

1 Like

Also, it’s doesn’t test if it equals 4, it sets it to 4.

2 Likes

so it is a matter of syntax? operator != being on par with strict inequality?

& therefore linguistically, i=1, >=4, doesn’t meet loop conditions even though the logic of the value negates the need for repetition in the loop?

i am just confused as to how (!=) doesnt equal whats natively unequal (1>4=)… and does such a syntax take precedent in all of JS or just for loops?

This has nothing to do with strictness.

With the loop

for (let i=1; i!=4; i+=2) {
  console.log("i=", i);
}

do you ever have i set to 4?

With the loop

for (let i=1; i<=4; i+=2) {
  console.log("i=", i);
}

what values do you see?

With the loop

for (let i=1; i>=4; i+=2) {
  console.log("i=", i);
}

what do you see?

The loop body is executed so long as the condition is true.

1 Like

for (let i=1; i!=4; i+=2) {
console.log(“i=”, i);
}

So if loops are made when conditions are True, may I ask how the given != creates a loop although it is as false as >=4, shouldn’t any number greater than 1 be regarded as != as well?

condition >=4 registers undefined in the terminal and not an infinite loop as is declared with the condition !=, is it a limitation of the FCC terminal?

console not terminal.

Did you run the code I provided? Did you look at the values printed out?

i!=4 is always true in the first loop. i>=4 is never true.

It has nothing to do with FCC. The two loops work the same everywhere.

ok, thanks alot for the clarification.

It is nice to meet you Dungeon Master!

i’m Dez, i am quite novice so expect to hear plenty from me! :sweat_smile:

1 Like