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

Random Numbers game

To continue off the Random Numbers game in Dave's JS course, I am trying to create a game that does the following:

The computer generates a random number between 0-100, and prompts the user to guess its number.

The user gets 5 guesses.

After each guess, the user receives a hint: If the number guessed is too low or too high, the computer tells the user this information and to guess again.

If the number guessed is correct, the computer sends an alert, indicating that you guessed correctly.

I am having difficulty getting my loop to run - should I use a for loop or a while loop? Either way, the loop doesn't iterate through the if statements, but only runs through once, then stops.

Can anyone help?

Thanks!

var randomNumber = Math.round(Math.random() *100 + 1);
var yourGuess = prompt("Guess a number between 1 and 100");
yourGuess = parseInt(yourGuess);
var guessesAllowed = 5;
var guessedCorrectly = false;

while (guessedCorrectly === false && guessesAllowed>0) {
  if(yourGuess > randomNumber) {
    var guessLess = prompt("choose a smaller number");
    if (guessLess === randomNumber){
      alert("You guessed the number!");
      guessedCorrectly = true;
    }

  }
  else if (yourGuess<randomNumber) {
    var guessMore = prompt("Choose a larger number");
    if (guessMore === randomNumber) {
      alert("You guessed the number!");
      guessedCorrectly = true;
    }
  }
  guessesAllowed--;
}

5 Answers

ummm, Jennifer. I made some correction to your original version of code.

var guessedCorrectly = false;
var randomNumber = Math.floor(Math.random() * 100) + 1;
var yourGuess;
var allowedGuesses = 5;
var message = "Guess a number between 1 and 100";

do {
  yourGuess = parseInt(prompt(message));
  allowedGuesses--;
  if (isNaN(yourGuess) || yourGuess < 1 || yourGuess > 100)  // check for illegal entry
    message = "Illegal entry, # of guesses left " + allowedGuesses;
  else if (yourGuess > randomNumber)
    message = "Your guess is too high, # of guesses left " + allowedGuesses;
  else if (yourGuess < randomNumber)
    message = "Your guess is too low, # of guesses left " + allowedGuesses;
  else {       // correctly guessed.
    guessedCorrectly = true;
    alert("You guessed correctly!");
    break;
  }
} while (allowedGuesses > 0);

if (!guessedCorrectly) {
  alert("sorry, you did not guess my number. My number was " + randomNumber);
}

http://jsfiddle.net/1dsf295e/

This version should have everything working correctly, one most noticeable change I made is that I assign value to a new variable message instead of prompt() for input and assign to yourGuess inside the if..else condition check.

Because I found that doing guessLess = prompt("choose a smaller number"); inside the if...else condition check really don't go well with the loop counter. And eventually you'll run into dilemma:

  1. either the user's 5th input never get a chance to be checked, even if it's correct, this is semantic error in the code.
  2. or you ask user for an extra 6th input, then discard it, this way the program should work correctly, but doesn't make much sense, and it's not an elegant solution.

Neither situation is desirable, well, as least that's the way I see it. Maybe Dave McFarland has better answer.

Use a while loop. Your for loop is actually happening 5 times i think.. Just without you realizing it. To check, console.log(i) in the loop;

Use the guessed correctly variable. It looks like that is not a part of your code.

Something like, while guessed correctly is false and allowable guesses is true, prompt for a new answer. You can still use a counter (i) to track the number of guesses.

while loop or do...while loop would be a better fit for this problem.

But you have to be careful.

while (condition) {
  if (condition) {
    yourGuess = prompt("something");
  }
  else if (condition) {
    yourGuess = prompt("something else");
  }
  allowedGuesses--;
}

Things can get tricky here, because if you prompt user to enter new guess within the if..else condition check, that can easily run into the issue that if a correct guess was made at the last attempt, that guess may never get a chance to be checked, and the program still sees guessedCorrect as false.

Thanks, William for your input. I will give it a try. Can I have if statements inside of do...while statements?

yes, of course.

There is where I am correctly at, but I still cannot seem to figure this out.

var randomNumber = Math.round(Math.random() *100 + 1);
var yourGuess = prompt("Guess a number between 1 and 100");
yourGuess = parseInt(yourGuess);
var guessesAllowed = 5;
var guessedCorrectly = false;

while (guessedCorrectly === false && guessesAllowed > 0) {
  if(yourGuess > randomNumber) {
    var guessLess = prompt("choose a smaller number");
    if (guessLess === randomNumber){
      alert("You guessed the number!");
      guessedCorrectly = true;
    }

  }
  else if (yourGuess<randomNumber) {
    var guessMore = prompt("Choose a larger number");
    if (guessMore === randomNumber) {
      alert("You guessed the number!");
      guessedCorrectly = true;
    }
  }
  else if (yourNumber === randomNumber) {
    alert("Good guess! You guessed correctly");
  }
  else {
    alert("Sorry, but you lose");
  }
  guessesAllowed--;
}

If the challenge is specific on 5 times, change your guessesAllowed to 4. Right now, you have 6 tries because 0 is included.

Thanks, William for your help. Although we might be getting closer to a solution, the above code that you supplied isn't quite right.

If the initial guess is incorrect, the user is never prompted to enter his second guess, and the game ends.

Hmm...

Although we might be getting closer to a solution, the above code that you supplied isn't quite right.

Hi, Jen, I guess you haven't tried out the code, right?. This isn't getting closer to a solution. It's one of the correct way you can solve this problem. http://jsfiddle.net/9snxvvej/2/