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
gameobject from the lesson earlier into yourREPL'sscript.js -
To make the
gameobject'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
numGuesseshas been removed because you will be adding aprevGuessesarray 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
prevGuessesproperty to thegameobject initialized to an empty array - Add a
getGuessmethod togamethat 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
getGuessmethod returns a value that is:- Is a number, not a string
- Is between
smallestNumandbiggestNum, inclusive -
Hints:
- This is a great use case for a
whileloop parseIntreturnsNaNif the string cannot be parsed into a number
- This is a great use case for a
- From within the
playmethod, invoke thegetGuessmethod and add the new guess to theprevGuessesarray -
Add a
rendermethod togamethatplaywill 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:
renderwon't be able to access any ofplay's local variables, e.g.,guess, so be sure passrenderany 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
joinmethod
-
- The
playmethod should end (return) when the guess matchessecretNum
Bonus
- When
playis run, immediately prompt the player to enter the smallest and biggest numbers instead of having them pre-set