Number Guessing Game

Hi, i’ve just finished making a simple Number Guessing Game, but there are some slight issues i would appreciate some help with.

  1. The first time you play the game (upon loading the browser) the textContent of the highOrLow variable does not display. However, when you press the play again button upon the game ending and play the game again, the high or low variable works fine and continues to work fine until you reload the browser and play for the “first” time again. I assume there is an issue with the if statement?

  2. Kind of the opposite problem, the first time you play the game and lose it, the highOrLow text content does not appear when the game over message displays (which is what i want), then when you restart a new game and lose, the game over message appears alonside the highOrLow message.

I hope i made myself clear, Thank you!

The two issues you describe are both related to the same thing. You will never when the game when you first play, because you never assign a value to randomNumber. The only time a value is assigned to randomNumber is when the game is over and the resetGame function is called. The last line of this function is the only time the generateNumber is called which generates the random number to guess. During the first round of play, randomNumber is undefined. Because undefined is neither greater or less than userGuess, the highOrLow.textContent does not get updated.

You need to assign randomNumber a value when the page loads and everything should work as you want.

1 Like

Thank you, that fixed the first issue but for some reason the highOrLow variable is still not getting converted to an empty string when the game is lost. (works fine when the game is won).

Seems like you solved your problem. I realize what you were asking now. I did not realize you wanted it to tell you if you were higher or lower after each number was submitted. I read your post as only show at the end, but since it did that, I assumed it was solved.

Your extra message each time is much better for game. Glad I could help you in some way.