Skip to Content

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

  1. You should have a function named getComputerChoice.
  2. 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. Use console.log() to print the result of the Math.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.

  1. Create a function named getHumanChoice.
  2. 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.

  1. Create two new variables named humanScore and computerScore in the global scope.
  2. 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.

  1. Create a new function named playRound.
  2. Define two parameters for playRound. Parameter one is humanChoice and parameter two is computerChoice. Use these two parameters to take the human and computer choices as arguments.
  3. Make your function’s humanChoice parameter case-insensitive so that players can input “rock”, “ROCK”, “RocK”, or other variations. Use the toLowerCase() method to make the string lowercase before comparing it.
  4. 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]".
  5. Increment the humanScore or computerScore 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.

  1. Create a new function named playGame.
  2. 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.
  3. 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!".

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

Loading...

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