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

Need help with RNG & Guess The Number game.

I'm doing the JS Basics course and I want to reverse the game that makes the user find the number that the script generated. So I want the user to think of a number and the script to correctly guess that number.

I can make the script continually generate a number in a range until the user confirms, but if I do that, script can generate the same numbers again and again. So I want to add the used numbers to an array and I don't want the script to ask about those numbers anymore. I'm having problems with placing and executing the test condition. (checking if the number exists in the array using indexOf method.)

Here's what I got so far:

var upper = 10;
var userConfirm = "NO";
var guess ;
var PCattempts = 0;
var usedNumbers = [];


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


alert("We'll play a guess the number game. First I will try to guess the number you're thinking.")
alert("Think of a number between 1-10.")

do {
    guess = getRandomNumber(upper);
    userConfirm = prompt("Is " + guess + " the number you were thinking? Please reply yes or no.");
    if (userConfirm.toUpperCase() !== "YES") {
    usedNumbers.push(guess);
}
    PCattempts = PCattempts + 1;
}

// The problem starts here, I can't figure out where to test usedNumbers.indexOf(guess) > -1.  
    while (usedNumbers.indexOf(guess) > -1 && userConfirm.toUpperCase() !== "YES"
        )


alert("So, it took me " + PCattempts + " attempts to guess your number.")

I'd appreciate if someone could help me tackle this within the limitations of 'javascript basics".

Shouldn't you be setting the upper limit of random so it doesn't guess a number greater than 10, to start?

@Michael Caveney The upper limit is already set, check line 1 please.

I think using Math.random to start with guesses, and then to push wrong guesses to an array is the wrong way to go. I feel that if the code was refactored so that you were using the Math.random function to pull guesses via index number from a previously populated array (with the numbers 1-10, obvi), and THEN to remove wrong numbers from the array would be a little simpler.

1 Answer

Steven Parker
Steven Parker
243,215 Points

:point_right: You can keep getting random numbers until you pick a fresh one.

This is probably the simplest method to program:

do {
  do {
    guess = getRandomNumber(upper);
  } while (usedNumbers.indexOf(guess) > -1)  // keep guessing until we get an unused one
  usedNumbers.push(guess);
  PCattempts = PCattempts + 1;
  userConfirm = prompt("Is " + guess + " the number you were thinking?  Please reply yes or no.");
  // keep guessing until we win, or have tried all 10 numbers.
} while (usedNumbers.length < 10 && userConfirm.toUpperCase() !== "YES")

You could check the exit condition and give a different message for "I got it" or "You tricked me". :stuck_out_tongue_winking_eye:

But this is not very efficient, as the more used numbers you have, the more time will be spent in the inner loop struggling to find an unused number. A better solution would be to create a list of all possible guesses, and each time one is picked remove it from the list. Then no inner loop would be needed, and the time to make a pick would remain constant for each try.
Update: This is what Michael suggested while I was composing.

Yes, I've read Michael's comment and I agree with both of you that creating the array beforehand and then using the numbers in the array to present the guess sounds like a better idea. I just wanted to see if I could get it resolved with the approach I had in the beginning, just for learning purposes. I didn't know how to use a loop in a loop earlier, so your code was actually very helpful! Thanks a lot! And if you don't mind, could you please tell me how to add an exit condition to an end of a loop like yours?