Created
July 31, 2014 21:22
-
-
Save sirgallifrey/0826e59ab09ca453d6f2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function getComputerChoice() { | |
var choice = Math.random(); | |
if (choice < 0.34) { | |
return "rock"; | |
} else if(choice <= 0.67) { | |
return "paper"; | |
} else { | |
return "scissors"; | |
} | |
} | |
function getUserChoice() { | |
var choice = prompt("Do you choose rock, paper or scissors?"); | |
while (choice != "rock" && choice != "paper" && choice != "scissors" ) { | |
choice = prompt("Please, choose rock, paper or scissors?"); | |
} | |
return choice; | |
} | |
function compare(choice1, choice2) { | |
if(choice1 == choice2) { | |
console.log ("The result is a tie!"); | |
} | |
else if(choice1 == "rock") { | |
if(choice2 == "scissors") { | |
console.log ("rock wins"); | |
} | |
else { | |
console.log ("paper wins"); | |
} | |
} | |
else if(choice1 == "paper") { | |
if(choice2 == "rock") { | |
console.log ("paper wins"); | |
} | |
else { | |
console.log ("scissors wins"); | |
} | |
} | |
else if(choice1 == "scissors") { | |
if(choice2 == "paper") { | |
console.log ("scissors win"); | |
} | |
else { | |
console.log ("rock win"); | |
} | |
} | |
} | |
var userChoice = getUserChoice(); | |
var computerChoice = getComputerChoice(); | |
console.log("Computer: " + computerChoice); | |
compare(userChoice, computerChoice); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment