-
-
Save jasongorman/adc64baeec54f31f2f0c08cf221b5442 to your computer and use it in GitHub Desktop.
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 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