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


Arikaturika Tumojenko
Arikaturika Tumojenko
8,897 Points

Did I get this right (the code running with examples)?

So, I want to see if I got the code correctly since I am a bit confused about the using or (!correctGuess).

            var randomNumber = getRandomNumber(10);
   // we define a variable which will hold the value of the random number 

    var guess;
   //we define a variable which will hold the answer we get from the user

    var guessCount = 0;
   //we define a variable which will hold the number of times the loop runs

    var correctGuess = false;
  //we define a variable and we set its value to false so we can make the while loop run (I    didn't really understand why do we set this value to false).

    function getRandomNumber (upper) {

        var num = Math.floor(Math.random() * upper) +1;
        return num;

   // the function generates a random  number, let's say 7

                do {

                    guess = prompt("I am thinking of a number between 1 and 10. Take a guess!")
                    guessCount +=1;

   // we ask the user to type a number; let's say the user types 5

                    if(parseInt(guess) === randomNumber) {
                        correctGuess = true; }

   // we check the condition in the if statement. 5 is not equal to 7, therefore correctGuess is false;

                } while(!correctGuess) 

//while correctGuess is false, the code goes back to the "do" block code again and it runs until the user's number is the same with the random number. The random number never changes, unless we reload the page.

            alert("You guessed the number! It took you " + guessCount + " times to guess")

//if the code from the "do" block code is true, the loop stops (the "while" block code never runs if the user guesses the number using his first try) and the alert message is displayed.

1 Answer

Steven Parker
Steven Parker
229,644 Points

It looks good to me. You set correctGuess to false at the beginning because the user has not guessed the number yet. Then, the loop repeats after each try while correctGuess stays false. If the number entered matches, correctGuess is set to true, which makes the looping end.

Does that make it clearer?

Arikaturika Tumojenko
Arikaturika Tumojenko
8,897 Points

Yes, definitely. I also re-watched the video few times and I guess I got them all :-). Thx.