For some reason I can’t wrap my head around this condition. Can someone please explain like I’m five?
I searched on here, read MDN documentation, but I feel like I’m missing something simple.
while (playerScore !== 5 && cpuScore !== 5)
So the && is saying that when one of these conditions evaluates to false, then the whole statement is false, correct?
And the while loop continues as long as the condition is true, right?
That’s absolutely correct. The
&& operator (also known as an "AND" operator) means that both parts of the statement need to be true for the whole statement to be true. Otherwise, the whole statement is false.
So as long as neither the player score and the CPU score are equal to 5 (meaning either one of them is either less than or greater than 5), the while loop will continue again and again.
The confusion is completely understandable, especially with the existence of the
! operators (also known as "NOT" operators).
Whenever I get confused with crafting statements like these, I try to break them down into the smallest chunks possible. With this statement, I would start with understanding the first half of the expression (
playerScore !== 5), then move to the second half (
cpuScore !== 5), and finally combine the two.
I hope that helps clear things up!