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 trialZachary grace
6,682 PointsGuessing game with Javascript
How would I make a game where you guess a number. And each number guessed is added to the previous guess? So if you guess 1 it says "you guessed: 1" and then of you guess 7 it says "you guessed: 2 8". Anyone have an idea?
2 Answers
Erik McClintock
45,783 PointsZachary,
This can be done in a few different ways, and all relatively simply! To get you started, let's break it down into the things that (off the bat) we know you'll need to build this out:
1) You'll need some form of accepting user input - this could be via an input element in your HTML, or perhaps via a JavaScript prompt
2) You'll need a couple of variables to store your data - one to hold onto the user's current guess, and one to keep track of the total of all the guesses
3) You'll need a way to display the results back to the user - this could be done via setting the innerHTML or innerText of an HTML element on the screen, or perhaps with a JavaScript alert or prompt
Hopefully this will help you get started! All you'll need to do from there is put your Treehouse training to use to piece the logic together.
Let me know if you need further assistance, and happy coding!
Erik
Erik McClintock
45,783 PointsZachary,
It looks and plays greatly! Very nicely done.
Now that you have the base game in place, are you considering expanding it at all? You could try:
1) Adding a set number of times a player is allowed to guess 2) Recording how many wins and how many losses a player has logged 3) Adding an event handler on the window to allow for guess submissions via a keydown of the Enter key
Looking forward to seeing what (if anything) you add, but it is great in its current state, too!
Erik
Erik McClintock
45,783 PointsErik McClintock
45,783 PointsNow, keep in mind, of course, that if you want it to truly be a guessing game, you'll need some hidden number for the player to be trying to guess! Thus:
1) You'll need another variable to hold the hidden number
2) You'll need a way to generate a new hidden number after each time the player successfully guesses it - you can do this simply with JavaScript's random function on the Math object (Math.random() - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random)
Additionally...
3) You'll probably want to provide a different message to the player based on whether or not they guessed your hidden number correctly, so you'll want to write in some logic to check that condition and display the win or lose messages accordingly.
4) You would also then want to create a function that would be called if the player guessed correctly whose use would be to restart the game (generate a new hidden number and clear out the currentGuess and totalGuess variables that you had been storing to throughout the game play).
Hope this makes sense!
Erik
Zachary grace
6,682 PointsZachary grace
6,682 Pointshttp://www.public.asu.edu/~srlee1/GIT417/GuesstheNumber/A3_Lee.html
This is what it looks like so far