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

do...while code won't execute

The following code won't execute properly in either the Firefox or Chrome browser. The prompt box appears and allows for a number entry, but there is no response as it never selects the correct number (even when testing a number from 1 to 3).

Two things I've noticed - in Firefox, the tab is always transferring data. It never seems to fully load. And in Chrome, it completely crashes the moment I prevent it from "creating any additional dialogues".

Also, in JSLint I get the following warning: "document.write can be a form of eval.". I'm not sure if this is throwing things off, as I am new to the DOM and how it operates.

Would anyone have an idea of the issue?

Thanks!

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("I'm thinking of a number between 1 & 10.  What is it?");
  guessCount += 1;
  if (parseInt(guess) === randomNumber) {
    correctGuess = true;
  }
} while( ! correctGuess )


document.write("<p>You guessed the correct number: " + getRandomNumber + "</p>");
document.write("<p>It took you " + guessCount + " tries to guess my number</p>");

On your first document.write statement you are calling the getRandomNumber function. You should be using the randomNumber variable instead.

2 Answers

Hi Dale. Thank you for the response. I made the change, but I'm still having the same issue unfortunately. Not sure what's going on.

Using the single line fix above, the program is working correctly for me using the Chrome browser. Are you still getting the same error?

I'm still having the same issue. It must be something on my end, as the code (as you said) is correct otherwise. I'll continue to dig deeper. Thanks for your help Dale.