JavaScript Project
Your game will be played against the computer. You will write a function that randomly returns "rock"
, "paper"
or "scissors"
.
You do not need to worry about the front-end part of the game. You will only write the logic for the game.
Requirements
- You should have a function named
getComputerChoice
. - Your
getComputerChoice
function should return"rock"
,"paper"
, or"scissors"
at random.
Hint: The
Math.random
method returns a random number that is greater than or equal to 0 and less than 1. Think about how you can use this to conditionally return one of the multiple choices. Useconsole.log()
to print the result of theMath.random
method to the console and see what you get.
Your game will be played by a human player. You will write a function that takes the user’s choice and returns it.
- Create a function named
getHumanChoice
. - Write the code so that
getHumanChoice
will return one of the valid choices depending on what the user inputs.
Hint: Use the
prompt()
method to get the user’s input. Take a look at the MDN documentation for more information.
Your game will keep track of the player’s score. You will write variables to keep track of the player’s score.
- Create two new variables named
humanScore
andcomputerScore
in the global scope. - Initialize those variables with the value of 0.
Your game will be played round by round. You will write a function that takes the human and computer player choices as arguments, plays a single round, increments the round winner’s score, and logs a winner announcement.
- Create a new function named
playRound
. - Define two parameters for
playRound
. Parameter one ishumanChoice
and parameter two iscomputerChoice
. Use these two parameters to take the human and computer choices as arguments. - Make your function’s
humanChoice
parameter case-insensitive so that players can input “rock”, “ROCK”, “RocK”, or other variations. Use thetoLowerCase()
method to make the string lowercase before comparing it. - Write the code for your
playRound
function that returns a string value representing the round winner.- If it is a tie, it should return
"It's a tie!"
. - If the player wins, it should return
"You win! [player choice] beats [computer choice]"
. - If the computer wins, it should return
"You lose! [computer choice] beats [player choice]"
.
- If it is a tie, it should return
- Increment the
humanScore
orcomputerScore
variable based on the round winner.
Your game will play 3 rounds. You will write a function named playGame
that calls playRound
to play 3 rounds, keeps track of the scores, and declares a winner at the end.
- Create a new function named
playGame
. - Create a loop that plays 3 rounds and calls the
playRound
function each time with the human’s choice and the computer’s choice functions as arguments. - At the end of the game, return the winner of the game based on who won the most rounds.
- If the human player wins more rounds than the computer player, return a message that says
"You win the game!"
. - If the computer player wins more rounds than the human player, return a message that says
"You lose the game!"
.
- If the human player wins more rounds than the computer player, return a message that says
Example Solution
Here is how the game should work, though yours can be more detailed in its implementation (your game may also begin immediately upon page load, without a button click):
Starter Code
Going Above and Beyond
Want to challenge yourself? Want to tie it all together - HTML, CSS, and JavaScript?
The following IS NOT REQUIRED as part of your tech prep work, but it is a great way to practice what you have learned so far and to challenge you to find new information that you have yet to encounter.
Try building the game with HTML, CSS, and JavaScript, using a fully styled user interface to play the game.
Like this (you may style your game however you like):
Rock, Paper, Scissors
Your Score
0
Computer's Score
0
Choose your weapon!
Round 1 of 3