Rock, Paper and Scissors Game using JavaScript

  1. var userChoice = prompt("Do you choose rock, paper or scissors?");  
  2. var computerChoice = Math.random();  
  3. if (computerChoice < 0.34)  
  4. {  
  5.     computerChoice = "rock";  
  6. }  
  7. else if (computerChoice <= 0.67)  
  8. {  
  9.     computerChoice = "paper";  
  10. }  
  11. else  
  12. {  
  13.     computerChoice = "scissors";  
  14. }  
  15. console.log("Computer: " + computerChoice);  
  16. var compare = function(choice1, choice2)  
  17. {  
  18.     if (choice1 === choice2)  
  19.     {  
  20.         return "The result is a tie!"  
  21.     }  
  22.     else if (choice1 === "rock")  
  23.     {  
  24.         if (choice2 === "scissors")  
  25.         {  
  26.             return "rock wins";  
  27.         }  
  28.         else  
  29.         {  
  30.             return "paper wins";  
  31.         }  
  32.     }  
  33.     else if (choice1 === "paper")  
  34.     {  
  35.         if (choice2 === "rock")  
  36.         {  
  37.             return "paper wins";  
  38.         }  
  39.         else  
  40.         {  
  41.             return "scissors wins";  
  42.         }  
  43.     }  
  44.     else if (choice1 === "scissors")  
  45.     {  
  46.         if (choice2 === "rock")  
  47.         {  
  48.             return "rock wins";  
  49.         }  
  50.         else  
  51.         {  
  52.             return "scissors wins";  
  53.         }  
  54.     }  
  55. }  
  56. if (userChoice === "scissors" || userChoice === "rock" || userChoice === "paper")  
  57. {  
  58.     compare(userChoice, computerChoice);  
  59. }  
  60. else  
  61. {  
  62.     confirm("Choose a valid option !! ")  
  63. }