Skip to content

Instantly share code, notes, and snippets.

@anthify
Created August 9, 2019 15:51
Show Gist options
  • Save anthify/78e99f454e2b9c722bcf29d9c2d3527d to your computer and use it in GitHub Desktop.
Save anthify/78e99f454e2b9c722bcf29d9c2d3527d to your computer and use it in GitHub Desktop.
Solution: Rock, Paper, Scissors
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