2. while syntax


#1

var understand = true;

while( ){
console.log("I'm learning while loops!");
understand = false;
}


#2

You need an input for the while loop that evaluates to true, like the understand value.

while(understand) {
//
}

#3

Well that was..deceptively simple.


#4

u must equate true to 1 and false to 0, then increment the value of the variable understand

var understand = 0;

while( understand===0 ){
console.log("I'm learning while loops!");
understand++;
}


#5

Not necessarily. You're using an integer to use a while loop, but you can also use booleans. Just ensure that you eventually set it to false or else you'll have an infinite loop.


#6

Sorry but can someone ELI5 this for me?
I thought this exercise should have been simple in that while understand was true then the message would pop up and then understand would be set to false. Like so:

While(understand = true) {
console.log("I'm learning while loops!");
understand = false;
}

This of course is incorrect but I don't know why. Why would just entering the understand variable name in the condition do anything? The way I read it, it says "So long as understand exists, then print the text and set the variable to false."

While(understand) {


#7

The problem is that this:

understand = true

assigns true to understand, but what you need is an operator that compares the two values for example == or ===.
And the reason why you can use understand as well is actually pretty simple in the end it does the same think about it:

understand === true is true for understand being false
understand === true is false for understand being false

so in the end the result of the comparison is always what understand initially was.


#8

And you don't need to check if understand is true in your while statements, while is always going to run so long as the input is true. So above the while statement, set understand to true, and then just put understand as the input, like so:

understand = true;
while (understand) {
//
}

#9

A post was split to a new topic: Infinite Loop