Skip to content

Instantly share code, notes, and snippets.

@jasongorman
Created April 3, 2020 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasongorman/adc64baeec54f31f2f0c08cf221b5442 to your computer and use it in GitHub Desktop.
Save jasongorman/adc64baeec54f31f2f0c08cf221b5442 to your computer and use it in GitHub Desktop.
const rock = "rock";
const scissors = "scissors";
const paper = "paper";
function play(game, player1, player2) {
let score = {...game.score};
let winner = undefined;
if (player1 === rock) {
playHand(player2, score, scissors, paper);
}
if (player1 === paper) {
playHand(player2, score, rock, scissors);
}
if (player1 === scissors) {
playHand(player2, score, paper, rock);
}
if (score.player1 === 2) {
winner = 1;
}
if (score.player2 === 2) {
winner = 2;
}
return {winner: winner, score: score};
function playHand(opponent, score, beats, losesTo) {
if (opponent === beats) {
score.player1++;
} else {
if (opponent === losesTo) {
score.player2++;
}
}
}
}
module.exports = {play, rock, paper, scissors};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment