Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Stefan Cutajar
Stefan Cutajar
7,747 Points

using ! operator

So Dave mentioned in the video that the ! (not operator) will change the value to the opposite so if: var correctGuess = false; do{ //////////// if (randomNumber === parseInt(guess){ correctGuesss = true; } while( ! correctGuess) //// So if ! changes the Boolean value to the opposite at the beginning false is changed to true so does that make it move to the last part of the program ? than if you manage to guess the number correct guess value changes to true so !correctGuess will become false (false isn't supposed to let the program start the loop again?)

3 Answers

Let's break this down and see it in detail to clear our concept about ! operator:

Code :

var correctGuess = false;

do{
//code
if (randomNumber === parseInt(guess){ correctGuesss = true; }
}while(!correctGuess)

Do-while loop lets you run the body of loop once at least even if the condition turns out to be false.

while(! correctGuess)

is same as

while ( correctGuess == false)

or in other words , keep on repeating the body of do-while loop as long as correctGuess is false.

! operator is same checking for equality of false but it's when you're checking for conditions:

! correctGuess is same as correctGuess == false;

But Dave is right , ! operator switches the value but not in case of while() or if condition,

Let's see this piece of code :

var check = false;
console.log (!check);

What will be the output on console? It will true ! Why ?

Well, !check is same as saying switch the value of check (if it's true then show it false and vice versa).

But in case of conditions of while and if and for loop , it works a bit different.

Finally this code,

var check = false;
check = ! check;
console.log(check);

It will also print value as True, because ! check is not being a part of any while, if or for loop condition in the above code.

I hope it answers your question, Happy Coding :)

  • Zain

Thanks so much for your in detail explanation. I finally now understand it. So basically the ! operator works differently in the loops.

Stefan Cutajar
Stefan Cutajar
7,747 Points

Thanks for your help I think I understand now :D The thing was confusing me that dave didn't said that it dose not work the same in loops.

Glad to help mate , Good Luck on future learning and also the techdegree :D I was going to start it as well because of those 12 projects but then I thought I should work on some individual tracks first. Currently doing Full Stack Javascript track

Stefan Cutajar
Stefan Cutajar
7,747 Points

Thanks buddy you too :) I started with the basic plan as well and I was in doubt if I should start the tech-degree or not but the fact that someone can correct your project I think it helps you to learn more but if you can't afford it you definitely can learn a lot with the basic plan :D