Created
August 9, 2019 15:51
-
-
Save anthify/78e99f454e2b9c722bcf29d9c2d3527d to your computer and use it in GitHub Desktop.
Solution: Rock, Paper, Scissors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const game = { | |
choices: ["rock", "paper", "scissors"], | |
rock: "scissors", | |
paper: "rock", | |
scissors: "paper" | |
}; | |
const play = playerChoice => { | |
if (!game.choices.includes(playerChoice)) { | |
return `Player hasn't selected valid choice`; | |
} | |
const computerChoice = game.choices[Math.floor(Math.random()*3)]; | |
if (computerChoice === playerChoice) { | |
return `Tie!`; | |
} | |
const outcome = game[playerChoice].includes(computerChoice) ? 'won' : 'lost'; | |
return `You ${outcome} with ${playerChoice}! Computer picked ${computerChoice}.`; | |
} | |
console.log(play("rock")); | |
console.log(play("paper")); | |
console.log(play("scissors")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment