Infinite Loop error


#1

Hello,

I keep getting an infinite loop error, though I can't figure out where I am going wrong. Anyone able to point it out? Thank you very much.


var status = true

for (var i = 0; i < 5; i++) {
console.log(i);
}
while(status) {
console.log("this text once");
status = false;
}


#2

I figured out that it was the while (status) at issue. I have corrected it with the below


var status = true

for (var i = 5; i > 0; i--) {
console.log(i);
}
while(status === true) {
console.log("this text once");
status = false;
}

However, now it won't print the "this text once" line. :frowning:


#3

This is interesting.
I don't think your change should make any difference (testing if a boolean is equal to true produces the same boolean), but then again I also don't think that's an infinite loop.

But I get an infinite loop as well.


#4

I don't why but your first code works if you chance the var status with another word


#5

Yeah, I just tried changing it out as well.

status is probably a built-in variable that can't be changed.

10/10 best language

But the thing is, when I print out status, it DOES say false as if it were changed. So..why then doesn't the loop stop, when the variable does appear to be changed?

Maybe it's converted to string? And non-empty strings are truthy

var status = true;
console.log(typeof false);
console.log(typeof status);

boolean
string

Again, JavaScript is clearly the best language there is.