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

why isnt the message for guessing right not coming up and is just a blank screen

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

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

do { guess= prompt('I am thinking of a number 1 and 10. what is it?');
guessCount +=1; if (parseInt(guess) === randomNumber) { correctGuess = true; } } while ( ! correctGuess ) document.write('<h1>You guessed the number!</h1>'); document.write('It took you ' + guessCount + ' tries to guess the number ' + randomNumber);

when i click to view it the box pops up to guess the number but after getting the right number it just goes to a blank screen and im stumped

2 Answers

Aakash Srivastava
Aakash Srivastava
5,415 Points

Hey friend Nick Rogers , you have done a good job . Everything is working correctly , just a minor mistake :
Instead of declaring correctGuess = false , you have declared guessCount = false .
guessCount variable is doing another job . So , here is the improved code:

var randomNumber = getRandomNumber(3);
var guess;
var guessCount = 0;
var correctGuess = false;          // you have declared guessCount = false 

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

do {
    guess = prompt('I am thinking of a number 1 and 10. what is it?');
    guessCount += 1;
    if (parseInt(guess) === randomNumber) {
        correctGuess = true;
    }
} while (!correctGuess) document.write('<h1>You guessed the number!</h1>');
document.write('It took you ' + guessCount + ' tries to guess the number ' + randomNumber);

Here is a little tip for you , whenever you face such problem , your first step should be to look in the Chrome developer tool . If you run your code , it clearly said correctGuess is not defined .
So , most of the time , chrome developer tool will help you.

Hope it helped :)

I downloaded the project files for this lesson on 9/15/2019 and had to fix index.html so this lesson could run. In case anyone else downloaded to run on their own code editor.

It's not necessary but the CSS isn't referenced correctly, the filepath is wrong:

<link rel="stylesheet" href="styles.css"> instead of:

<link rel="stylesheet" href="css/styles.css">

The JS file isn't called correctly it's a completely different name, let alone being called in the right folder:

<script src="app.js"></script> instead of:

<script src="js/script.js"></script>