Last active
June 1, 2016 11:22
-
-
Save devlemire/cee9f624c64736f5ad0524ce3ad9da57 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
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