Advertisement
Guest User

Untitled

a guest
Aug 27th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var userChoice = prompt("Do you choose rock, paper or scissors?");
  2. var computerChoice = Math.random();
  3. if (computerChoice < 0.34) {
  4.     computerChoice = "rock";
  5. } else if(computerChoice <= 0.67) {
  6.     computerChoice = "paper";
  7. } else {
  8.     computerChoice = "scissors";
  9. }
  10.  
  11. console.log("Computer chose: " + computerChoice);
  12. console.log("You chose: " + userChoice);
  13.  
  14. var compare = function(choice1, choice2) {
  15.     if(choice1 === choice2) {
  16.         console.log("The result is a tie!");
  17.     }
  18.     else if(choice1 === "rock") {
  19.         if(choice2 === "scissors") {
  20.             return "rock wins";
  21.         }
  22.         else {
  23.             return "paper wins";
  24.         }
  25.     }
  26.     else if(choice1 === "paper") {
  27.         if(choice2 === "rock") {
  28.             return "paper wins";
  29.         }
  30.         else {
  31.             return "scissors wins";
  32.         }
  33.     }
  34.     else if(choice1 === "scissors") {
  35.         if(choice2 === "rock") {
  36.             return "rock wins";
  37.         }
  38.         else {
  39.             return "scissors wins";
  40.        
  41.         }
  42.     }
  43. };
  44.  
  45. if(userChoice !== "rock") {
  46.     return "Your choice is invalid!";
  47. } else if(userChoice !== "paper") {
  48.     return "Your choice is invalid!";
  49. } else
  50.  
  51.     console.log(compare(userChoice, computerChoice));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement