Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ELIF – JavaScript – do/while loop in rock, paper, scissors – the while condition I'm using is making my head hurt

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading