Wednesday, February 19, 2014

rock paper scissors

var userChoice = prompt("Do you choose rock, paper or scissors?"); if (userChoice!="rock" && userChoice!="paper" && userChoice!="scissors") { userChoice = prompt("please enter a valid choice"); }; var computerChoice = Math.random(); if (computerChoice < 0.34) { computerChoice = "rock"; } else if(computerChoice <= 0.67) { computerChoice = "paper"; } else { computerChoice = "scissors"; } var compare=function(choice1, choice2) { if (choice1===choice2) { return"The result is a tie!"; } else if(choice1==="rock") { if (choice2==="scissors") { return "rock wins"; } else { return "paper wins"; } } else if(choice1==="paper") { if(choice2==="rock") { return "paper wins"; } else { return "scissors wins"; } } else { if(choice2==="rock") { return "rock wins"; } else { return "scissors wins"; } } }; console.log(compare(userChoice, computerChoice)); Play Game!

2 comments:

  1. Jonathan, please resist the temptation to post compressed JavaScript! Great idea posting a link to a playable version of the game. We will discuss using alert on Tuesday instead of console.log to make it much easier for the user to see the results.

    ReplyDelete
  2. It must be compressing automatically when I paste it in

    ReplyDelete