A little JavaScript virtual version of the famous game "Rock, Paper, Scissors, Shoot!"
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34){
computerChoice = "rock";
}else if(computerChoice <= 0.67){
computerChoice = "paper";
}else{
computerChoice = "scissors";
}
var results = {
'scissors':{'paper': "you win!", 'rock': 'you lose...'},
'rock': {'paper': 'you lose...','scissors':'you win!' },
'paper': {'scissors':'you lose...','rock': 'you win!'}
}
if(userChoice == computerChoice){
document.write("It's a tie!");
}else{
document.write(results[userChoice][computerChoice]);
}
document.write("<a href='#' onClick='window.location.reload(true);'>Try again</a>");
link: http://jsfiddle.net/kYRFU/31/
Written by JOSE CARLOS
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Code
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#