Created
May 14, 2015 14:11
-
-
Save stephen-maina/c5ef436d14cd3761009a 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
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