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

Eduardo Vargas
Eduardo Vargas
4,928 Points

Replacing the NOT OPERATOR with ( correctGuess === false )?

Like a lot of people here, the NOT OPERATOR on this exercise kinda through me for a loop (no pun intended). After staring at it for about 30 minutes or so, it began to make some sense. With that said, I replaced ( ! correctGuess ) with ( correctGuess === false ). Isn't this pretty much the same thing and a bit more straight forward?

while ( correctGuess === false )

I ran the the code and it seems to work.

var randomNumber = getRandomNumber(10);
var guess;
var guessCount = 0;
var correctGuess = false;

function getRandomNumber( upper ) {
  var num = Math.floor(Math.random() * upper) + 1; 
  return num;
}

do {
    guess = prompt("Guess a number from 1 - 10. What is it?");
    guessCount += 1;
    if ( parseInt( guess ) === randomNumber ) {
        correctGuess = true;
        }
} while ( correctGuess === false )

3 Answers

Roc Gabrielse
seal-mask
.a{fill-rule:evenodd;}techdegree
Roc Gabrielse
Full Stack JavaScript Techdegree Student 6,979 Points

You are correct in that it provides the exact same functionality and how you write it is ultimately a matter of preference. If using the comparison operator makes it more readable to you, then by all means. The ! operator is just a shorthand way of writing it and that's what the teacher is trying to convey. It's very common in development and there's actually a term for situations like these, called syntactic sugar. You'll notice he uses syntactic sugar in several other areas of this course and previous courses. Here's a very basic example of syntactic sugar in use:

var number = 1; // Initialize our variable

// Compare these two lines
number = number + 1; // Add 1 to number, and store it back into number, which now holds the value of 2
number += 1; // Same functionality as the line above, but shorthand and easier way of writing it, "syntactic sugar"

It's one of those things you'll get used to the more you code, especially when you're handling other people's code.

Your correctGuess variable is intialized to false and only has one statement that would reassign it to true so those are the only two possible values, so yes the results would be the same.

Eduardo Vargas
Eduardo Vargas
4,928 Points

Ah, cool. With that cleared up, what is the advantage or purpose of writing it the way Dave did? I feel like I'm missing something by dismissing it as mere preference.

Duncan Murtagh
Duncan Murtagh
1,557 Points

I was wondering the same Eduardo.

A follow on question, does: while ( ! correctGuess ) ....mean that correctGuess is False, regardless of whether or not you earlier declared: var CorrectGuess = false; ???