JavaScript Objects Lab: Guess The Number Game
Intro
This lab will provide you with practice working with JavaScript objects, plus some additional practice working with arrays.
This lab builds upon the game
object that was started in the Intro to JS Objects lesson.
This lab is a deliverable
Lab Setup
For this lab, create a HTML, CSS & JS
REPL
from repl.it -- you can name it "JavaScript Objects Lab"
.
- Copy over the work already done on the
game
object from the lesson earlier into yourREPL's
script.js
-
To make the
game
object's code 'cleaner', let's move the properties that were added separately during the lesson, into the object literal so that it looks like this:const game = { title: 'Guess the Number!', biggestNum: 100, smallestNum: 1, secretNum: null, play: function() { this.secretNum = Math.floor(Math.random() * (this.biggestNum - this.smallestNum + 1)) + this.smallestNum; } };
Note that
numGuesses
has been removed because you will be adding aprevGuesses
array whose length can be used to obtain the number of guesses when needed.
- To begin running your code and checking its output, click the
[run]
button.
Features
- Allow the player to continually be prompted to enter their guess of what the secret number is until they guess correctly
-
If the player has an incorrect guess, display an alert message that informs the player:
- Whether their guess is too high, or too low, and...
- A list of all the previously guessed numbers (without showing the square brackets of an array)
-
If the player has guessed the secret number:
- Display an alert message that congrats the player and informs them of how many guesses they took
- End the game play
Tasks
Completing the following tasks will result in a working Guess the Number game:
- Add a
prevGuesses
property to thegame
object initialized to an empty array - Add a
getGuess
method togame
that prompts the player to enter a guess with a message formatted as: Enter a guess between [smallestNum] and [biggestNum]:. Hint - use a template literal for the prompt message -
Ensure that the
getGuess
method returns a value that is:- Is a number, not a string
- Is between
smallestNum
andbiggestNum
, inclusive -
Hints:
- This is a great use case for a
while
loop parseInt
returnsNaN
if the string cannot be parsed into a number
- This is a great use case for a
- From within the
play
method, invoke thegetGuess
method and add the new guess to theprevGuesses
array -
Add a
render
method togame
thatplay
will call after a guess has been made that alerts:-
If the secret has been guessed:
Congrats! You guessed the number in [x] guesses!
-
Otherwise:
Your guess is too [high|low] Previous guesses: x, x, x, x
-
Hints:
render
won't be able to access any ofplay
's local variables, e.g.,guess
, so be sure passrender
any arguments as needed- Template literals not only have interpolation, they honor whitespace - including line breaks!
- The list of previous guesses can be generated using the array
join
method
-
- The
play
method should end (return
) when the guess matchessecretNum
Bonus
- When
play
is run, immediately prompt the player to enter the smallest and biggest numbers instead of having them pre-set