Let's go ahead and set up our environment for creating the number guessing game.
As we've done before, open up your hack-camp
workspace and create a new folder
with the name number-guesser-game
.
Now let's create a file called index.html
to put our HTML into and open it.
Now let's go ahead and type (not copy and paste) the following website template
into index.html
.
<!DOCTYPE html>
<html>
<head>
<title>Number Guesser Game</title>
<script src="main.js"></script>
</head>
<body>
</body>
</html>
This sets up a basic webpage and tells it to include the JavaScript file
main.js
. Your workspace should now look like the following:
But there's one issue with our website... main.js
doesn't exist yet! Let's go
ahead and create it.
Great! Now we're all set up. The remainder of this guide will take place in
main.js
(we won't be playing with the HTML).
- We'll be breaking the game down into a few different parts:
- Starting the game
- Coming up with random number for the user to guess
- Taking a guess from the user and testing if it is right
-
The first step of the game is to run the game. Let's make a function for it:
function startGame() { console.log('game running!') }
Now that we can start our game, it needs to have a random number for the player to guess.
-
We want a random number. We can do this with
Math.random()
, which will return a random number from 0 to 1.function getRandomNumber() { return Math.random(); }
-
Our
getRandomNumber()
function returns a number from 0 to 1. How can we get it to return a number from 0 to 20 using multiplication?function getRandomNumber() { return Math.random() * 20; }
-
Currently our
getRandomNumber()
function returns anything between 1 and 20, including hard-to-guess numbers like3.1293129321
and12.5921332
. We want to round these to3
and13
. We can round our numbers withMath.round()
.function getRandomNumber() { return Math.round(Math.random() * 20); }
-
We want to access this random number from our game, so we'll assign it to a variable inside
startGame()
function getRandomNumber() { return Math.round(Math.random() * 20); } function startGame() { console.log('game running!'); var answer = getRandomNumber(); }
Let's make a function that asks the user for a guess. It's input will be the
correct answer from the startGame()
function. It will then ask the user for a
guess, and tell them if it was too low, too high, or correct. If the user's
guess was correct the function will not keep running, but if the guess was wrong
the function will keep running.
-
Make a new function called
checkGuess()
- Make sure this is before the
startGame()
function
- Make sure this is before the
-
The function will need to ask the user for a guess. We can do this with
prompt()
. InsidecheckGuess()
call theprompt()
function with the argument'Make a guess!'
.function checkGuess(correctAnswer) { prompt('Make a guess!'); }
-
The
prompt()
function returns what the user types in. We'll use it later, so let's store it in a variable calleduserGuess
.function checkGuess(correctAnswer) { var userGuess = prompt('Make a guess!'); }
-
Now we need to check if the user's guess was correct. We can do this by checking if
userGuess == correctAnswer
.function checkGuess(correctAnswer) { var userGuess = prompt('Make a guess!'); if(userGuess == correctAnswer) { alert('You won!'); } }
-
Let's also check if the user's guess is too high (
userGuess > correctAnswer
), or too low (userGuess < correctAnswer
).function checkGuess(correctAnswer) { var userGuess = prompt('Make a guess!'); if(userGuess == correctAnswer) { alert('You won!'); } else if(userGuess > correctAnswer) { alert('Too high!'); } else if(userGuess < correctAnswer) { alert('Too low!'); } }
-
Now we need to make sure the
checkGuess()
will keep asking the user or new guesses when their guess is wrong. Go to thestartGame()
function and add awhile
loop.function startGame() { console.log('game running!'); var answer = getRandomNumber(20); while(checkGuess(answer)) { } }
- This will infinitly run
checkGuess(answer)
as long ascheckGuess(answer)
returnstrue
. If it ever returnsfalse
the while loop will end.
- This will infinitly run
-
We want our
checkGuess()
function to returntrue
whenever the user makes an incorrect guess so it will run again. We want ourcheckGuess()
function to returnfalse
when the user guesses correctly, so the loop will end and we can tell the player they won.function checkGuess(correctAnswer) { var userGuess = prompt('Make a guess!'); if(userGuess == correctAnswer) { alert('You won!'); return false; // we want to return false when the user guesses correctly } else if(userGuess > correctAnswer) { alert('Too high!'); return true; // we want to return true when the user guesses wrong } else if(userGuess < correctAnswer) { alert('Too low!'); return true; // we want to return true when the user guesses wrong } }
-
Try the game!
-
Let's add a score to the game. We want to track the number of turns that occur, so let's make a new variable
turnCounter
to keep count.function startGame() { console.log('game running!'); var answer = getRandomNumber(); var turnCounter = 1; while(checkGuess(answer)) { } }
-
Let's increase the turnCounter everytime the user makes a guess. We can do this by adding 1 to turnCounter inside the while loop because everything inside the while loop will get run once everytime the loop runs.
function startGame() { console.log('game running!'); var answer = getRandomNumber(); var turnCounter = 1; while(checkGuess(answer)) { turnCounter = turnCounter + 1; } }
-
Now we have to tell the user they won at the end. We can do this by calling an
alert()
function after our while loop instartGame()
because we'll only run stuff after the loop when the user guesses correctly andcheckGuess(answer)
returnsfalse
.function startGame() { console.log('game running!'); var answer = getRandomNumber(); var turnCounter = 1; while(checkGuess(answer)) { turnCounter = turnCounter + 1; } alert('You guessed the number in ' + turnCounter + ' turns.'); }