If true = 1 in script why isn't this accepted


#1


... Already explained in the title. Simple question (Well I hope it is :wink:)


var understand = true;

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


#2

In this situation, true does not directly translate to 1. They are both literal.

We don't need a comparison since understand is already a boolean.

while (understand) {

}

If you don't want false to echo to the screen, switch the two lines around and set understand before logging the output.

    understand = false
    console.log( ... )

Have you learned about logical operators yet? There is another way to write the assignment... toggle the boolean:

understand = !understand

Ignore this if you haven't covered the ! (NOT) operator, yet. It will come up shortly.


#3

Thanks on both occasions (you helped me on 2 posts). Eh I haven't learn about "logical operators" yet, but I was reading a bit of the glossary for java-script and came across something along the lines that made me put "! = opposite value" in a set of notes. Sounds like there's a section for it so I won't stress about the exact meaning yet!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.