Skip to content

Instantly share code, notes, and snippets.

@stephen-maina
Created May 14, 2015 14:11
Show Gist options
  • Save stephen-maina/c5ef436d14cd3761009a to your computer and use it in GitHub Desktop.
Save stephen-maina/c5ef436d14cd3761009a to your computer and use it in GitHub Desktop.
var compare=function(user,comp){
if(user===comp){
return "The result is a tie!";
}else if(user==="rock"){
if(comp==="scissors"){
return "rock wins";
}else{
return "paper wins";
}
}else if(user==="paper"){
if(comp==="rock"){
return "paper wins";
}else{
return "scissors wins"
}
}else{
if(comp==="rock"){
return "rock wins";
}else{
return "scissors wins";
}
}
};
var userChoice = prompt("Do you choose rock, paper or scissors?");
if (userChoice !=="rock"||userChoice !=="paper"||userChoice !=="scissors"){
console.log("invalid input");
}
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} 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