Before you start coding:
- create another git branch called your name
- finish your code in
index.js
. Do not update any other file. - commit
- push it to GitHub
- open the repository and create pull request
- request my (@bahriddin) code review.
Rock paper scissors is a classic two player game. Each player chooses either rock, paper, or scissors. The items are compared, and whichever player chooses the more powerful item wins.
The possible outcomes are:
- Rock destroys scissors.
- Scissors cut paper.
- Paper covers rock.
- If there’s a tie, then the game ends in a draw.
Our code will break the game into four parts:
- Get the user’s choice.
- Get the computer’s choice.
- Compare the two choices and determine a winner.
- Start the program and display the results.
- The user should be able to choose ‘rock’, ‘paper’, or ‘scissors’ when the game starts. Since a user can pass in a parameter, such as ‘Rock’ ‘rock’, ' rock ' with different capitalizations and spaces between. So, your
getUserChoice()
function should:
- clean all cases. For example,
userInput
should berock
no matter what capitalization or extra spaces used. - return input if it is valid input that is one of rock, paper, scissors.
- if input is invalid, it should not return anything but log some error message of your choice.
-
Now we need to have the computer make a choice. Inside
getComputerChoice()
utilizeMath.random()
andMath.floor()
to get a whole number between 0 and 2. Then, depending on the number, return either 'rock', 'paper', or 'scissors'. -
Now it’s time to determine a winner.
determineWinner()
will compare the two choices played and then return if the human player won, lost, or tied.
- Let’s deal with the tie condition first. Within the
determineWinner()
function, write an if statement that checks if the userChoice parameter equals the computerChoice parameter. If so,return
a string that the game was a 'tie'. - If the game is not a tie, you’ll need to determine a winner. Using
if else
and/orswitch
statement figure out whether 'user won' or 'computer won' and return this string.
- Everything is set up. Now you need to start the game and log the results.
-
Inside the
playGame()
function, create a variable nameduserChoice
set equal to the result of callinggetUserChoice()
, passing in either 'rock', 'paper', or 'scissors' as an argument. -
Create another variable named
computerChoice
, and set it equal to the result of callinggetComputerChoice()
. Under both of these variables, useconsole.log
to print them to the console. -
Finally, let’s determine who won. Inside the
playGame()
function, call thedetermineWinner()
function. Pass in theuserChoice
andcomputerChoice
variables as its parameters. Make sure to put this function call inside of aconsole.log()
statement so you can see the result.Then, to start the game, call the
playGame()
function on the last line of your program.
- Make this game better by adding a secret cheat code. If a user types 'bomb' as their choice, then make sure they win, no matter what.
In order to run test cases you should install
- Node.JS. Pick Recommended For Most Users version.
- Yarn. Just follow instructions. Follow global install.
Then inside the folder open Git Bash
and type yarn
. Wait until it finishes.
Now you should be able to run test scripts by running yarn test
in Git Bash. If you haven't started coding, it should show you all red failures. After you successfully finish the project you should see all green test passes :).
You have another way of running tests: yarn watch
. It will listen to your code changes and run tests every time you modify it. If you want to exit this process just type q
. For other info, read the information on the screen.