Advertisement
Guest User

Untitled

a guest
Aug 27th, 2014
179
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.  
  3. var computerChoice = Math.random();
  4. if (computerChoice < 0.34) {
  5.     computerChoice = "rock";
  6. } else if(computerChoice <= 0.67) {
  7.     computerChoice = "paper";
  8. } else {
  9.     computerChoice = "scissors";
  10. }
  11.  
  12. console.log("Computer chose: " + computerChoice);
  13. console.log("You chose: " + userChoice);
  14.  
  15. var compare = function(choice1, choice2) {
  16.     if(choice1 === choice2) {
  17.         return "The result is a tie!";
  18.     }
  19.     else if(choice1 === "rock") {
  20.         if(choice2 === "scissors") {
  21.             return "rock wins";
  22.         }
  23.         else {
  24.             return "paper wins";
  25.         }
  26.     }
  27.     else if(choice1 === "paper") {
  28.         if(choice2 === "rock") {
  29.             return "paper wins";
  30.         }
  31.         else {
  32.             return "scissors wins";
  33.         }
  34.     }
  35.     else if(choice1 === "scissors") {
  36.         if(choice2 === "rock") {
  37.             return "rock wins";
  38.         }
  39.         else {
  40.             return "scissors wins";
  41.         }
  42.        
  43.     }
  44. };
  45.    
  46. console.log(compare(userChoice, computerChoice));
  47.  
  48. var invalidChoice = function(userChoice) {
  49.     if(userChoice !== "rock") {
  50.         return "Your choice is invalid!";
  51.     }
  52.     else if(userChoice !== "paper") {
  53.         return "Your choice is invalid!";
  54.     }
  55.     else if(userChoice !== "scissors") {
  56.         return "Your choice is invalid!";
  57.     }
  58. };
  59.  
  60. console.log(invalidChoice(userChoice));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement