Created
February 3, 2019 14:48
-
-
Save jorenbroekema/085b567b0b1733776dd65da3ea034113 to your computer and use it in GitHub Desktop.
Rock paper scissors prompt game
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
alert('Welcome to the Rock Paper and Scissors game!'); | |
playGame(); | |
function playGame() { | |
var choice = prompt('What will you play? rock, paper or scissors?'); | |
var opponentChoice = Math.ceil(Math.random() * 3); | |
switch(opponentChoice) { | |
case 1: opponentChoice = 'paper'; | |
case 2: opponentChoice = 'rock'; | |
case 3: opponentChoice = 'scissors'; | |
} | |
var playAgain; | |
if (choice === opponentChoice) { | |
playAgain = confirm('Opponent had: ' + opponentChoice + '. DRAW!'); | |
} else if (choice === 'paper' && opponentChoice === 'rock') { | |
playAgain = confirm('Opponent had: ' + opponentChoice + '. YOU WON :)!'); | |
} else if (choice === 'paper' && opponentChoice === 'scissors') { | |
playAgain = confirm('Opponent had: ' + opponentChoice + '. YOU LOST :(!'); | |
} else if (choice === 'rock' && opponentChoice === 'scissors') { | |
playAgain = confirm('Opponent had: ' + opponentChoice + '. YOU WON :)!'); | |
} else if (choice === 'rock' && opponentChoice === 'paper') { | |
playAgain = confirm('Opponent had: ' + opponentChoice + '. YOU LOST :(!'); | |
} else if (choice === 'scissors' && opponentChoice === 'paper') { | |
playAgain = confirm('Opponent had: ' + opponentChoice + '. YOU WON :)!'); | |
} else if (choice === 'scissors' && opponentChoice === 'rock') { | |
playAgain = confirm('Opponent had: ' + opponentChoice + '. YOU LOST :(!'); | |
} | |
if (playAgain) { | |
playGame(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment