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

I'm having some trouble with my random number game with JavaScript.

Everything in this game goes perfect up until the last part. If the player gets it right, they get a point to the streak and the system creates a new random number, thats fine. The higher/lower else if statements work too; popping up a message respective of the users guess. I have a variable named "guessTrack" that increases by 1 per click of the submit button. Its increasing fine per click. I used an else if statement to reset the users streak, random number and change the message when they have had 2 guesses. But it doesnt work and I have no idea why.

//fuction to create random number
function getRandomNum() {
    var randomnumber =Math.floor(Math.random() * 6 ) +1;
    return randomnumber;
}
//Sets the global score and other variables. Writes basic text to the screen
var streak = 0;
var ranNum = getRandomNum();
var guess= 0;
var guessTrack = 0;


var openmessage = "I'm thinking of a number between 1 and 6...";
document.getElementById("score").innerHTML = streak;
document.getElementById("gameMessage").innerHTML = openmessage;

//this is the submit button where the programme takes place.(the console.log is just for my benifit whilst developing.)
function getAnswer() {
    var firstGuess =(document.getElementById("user").value);
    guess = parseInt(firstGuess);
    guessTrack +=1;
    console.log("guess " + guess);
    console.log("track "+ guessTrack);
    console.log("Random num " + ranNum);
    console.log("Streak "+ streak)

    //If the guess is correct, +1 streak and create a new random number

    if (guess === ranNum){
        document.getElementById("gameMessage").innerHTML = "Nice guess, you got it right! Have another go. Im thinking of a number between 1 and 6...";
        streak += 1;
        document.getElementById("score").innerHTML = streak;
        guessTrack = 0;
        ranNum = getRandomNum();

    // if the guess is HIGHER or LOWER these messages pop up respective of guess value.

    } else if (guess > ranNum) {
       document.getElementById("gameMessage").innerHTML =  "The number I am thinking of is LOWER than " + guess + ".";

    } else if (guess < ranNum) {
        document.getElementById("gameMessage").innerHTML =  "The number I am thinking of is HIGHER than " + guess + ".";

    //this is the final peice of code that doesnt work. if guess is not right after 2 trys, the streak resets and player starts again.

    } else if (guessTrack === 2  ) {
            document.getElementById("gameMessage").innerHTML =  "Sorry you didnt guess the number, your streak has been reset. Try again. I'm thinking of a number between 1 and 6...";
            streak = 0;
            document.getElementById("score").innerHTML = streak;
            guessTrack = 0;
            ranNum = getRandomNum();
            }     
 }

1 Answer

Steven Parker
Steven Parker
243,228 Points

You forgot to include your HTML, but I guessed at it based on the JavaScript. I think this issue is best explained by the following commented excerpt from your code:

  if (guess === ranNum) {
    // this code runs if the guess is correct
  } else if (guess > ranNum) {
    // this code runs if the guess is NOT correct and too high
  } else if (guess < ranNum) {
    // this code runs if the guess is NOT correct and NOT too high and too low
  } else if (guessTrack === 2) {
   // this code runs if the guess is NOT correct and NOT too high and NOT too low and guessTrack is 2
   // ... which is never, since the guess must be one of the first 3 conditions
  }

:point_right: You probably don't want that last else.

You could optionally leave off the if (guess < ranNum) above that, since it must be true if the first two conditions failed.

Another option, regardless of the outcome, you might want to clear the input field after each guess:

document.getElementById("user").value = "";

Thanks for the reply. I know I can just have 3 statements in the if statement, the second else if can just be changed to else but i still need the score streak to reset if the player has had 2 guesses (one initial guess and one with a hint). Along with the win streak resetting upon not guessing also I still need the random number to change and a message to pop up saying "you didn't guess correct, please try again". this way the players win streak is over and the program is reset and they can play again. The code does this perfect with the first if statement and resets the code ECT. I just cant get the code to work when the player fails. At the moment they can just keep guessing, using the hints until they get the correct number and the first if statement runs, giving them the point and resetting