Skip to content

Instantly share code, notes, and snippets.

@sirgallifrey
Created July 31, 2014 21: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 sirgallifrey/0826e59ab09ca453d6f2 to your computer and use it in GitHub Desktop.
Save sirgallifrey/0826e59ab09ca453d6f2 to your computer and use it in GitHub Desktop.
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