Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops `do ... while` Loops

Joshua Swilling
Joshua Swilling
1,135 Points

do-while loop

I am curious and want to ask, can any operator be used to test conditions within the " while( ____ )" half of the loop?

2 Answers

Juan Naputi
Juan Naputi
1,771 Points

Yup! All of the conditional operators (===, !==, >, <, >=, <=) are valid inside of the "while()" half of the loop.

Andrew Baran
Andrew Baran
Courses Plus Student 5,089 Points

Can you tell me how come this doesn't work? while ( !== correctGuess)

and why this doesn't work either?

while ( guess ! correctGuess)

Thank you

Juan Naputi
Juan Naputi
1,771 Points

For while ( !== correctGuess), you have to provide a value that you want to compare. Typical setup would be: while ([!] {variableToCheck} {comparisonOperator} {valueToCheckAgainst}). With this template, you are missing the {variableToCheck} part of your while-loop.

For the second one, while (guess ! correctGuess), you have to use one of the valid comparison operators (===, !==, ==, !=, >=, <=). If you don't have these, it is an invalid comparison. The only time you will be using an not [!] comparison on its own is if you are looking for a false boolean value: while ( !isTrue ).

I can expand the context if needed.

Hope that helps!

*** For the first answer, the [!] at the front means that it is optional and is used when you are checking for a false boolean value. Refer to the second answer for a reference.