Skip to content

Instantly share code, notes, and snippets.

@jorenbroekema
Created February 3, 2019 14:48
Show Gist options
  • Save jorenbroekema/085b567b0b1733776dd65da3ea034113 to your computer and use it in GitHub Desktop.
Save jorenbroekema/085b567b0b1733776dd65da3ea034113 to your computer and use it in GitHub Desktop.
Rock paper scissors prompt game
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