Skip to content

Instantly share code, notes, and snippets.

@jasongorman

jasongorman/game.js Secret

Created Apr 3, 2020
Embed
What would you like to do?
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