Last Updated: February 25, 2016
·
2.84K
· josecarlospsh

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/