Skip to content

Instantly share code, notes, and snippets.

@devlemire
Last active June 1, 2016 11:22
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 devlemire/cee9f624c64736f5ad0524ce3ad9da57 to your computer and use it in GitHub Desktop.
Save devlemire/cee9f624c64736f5ad0524ce3ad9da57 to your computer and use it in GitHub Desktop.
var selection = ["Rock", "Paper", "Scissors"];
var player = playerChoice();
var computer = computerChoice();
console.log(compare(player, computer));
function playerChoice() {
var choice;
do {
choice = prompt("Rock(1), Paper(2), or Scissors(3)?").toLowerCase();
switch(choice) {
case 'rock':
choice = 1;
break;
case 'paper':
choice = 2;
break;
case 'scissors':
choice = 3;
break;
default:
choice = parseInt(choice);
}
} while (choice !== 1 && choice !== 2 && choice !== 3);
console.log("Player Choice: " + selection[choice - 1] + ".")
return choice;
}
function computerChoice() {
var choice = Math.floor(Math.random() * 3 + 1);
console.log("Computer Choice: " + selection[choice - 1] + ".");
return choice;
}
function compare(player, computer) {
switch(player) {
case 1:
switch(computer) {
case 1:
return "The game resulted in a tie.";
case 2:
return "The computer won.";
case 3:
return "You won.";
}
break;
case 2:
switch(computer) {
case 1:
return "You won.";
case 2:
return "The game resulted in a tie.";
case 3:
return "The computer won.";
}
break;
case 3:
switch(computer) {
case 1:
return "The computer won.";
case 2:
return "You won.";
case 3:
return "The game resulted in a tie.";
}
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment