Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
65
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.33) {
  4.     computerChoice = "rock";
  5. } else if(computerChoice >= 0.66) {
  6.     computerChoice = "paper";
  7. } else {
  8.     computerChoice = "scissors";
  9. } console.log("Computer: " + computerChoice);
  10.  
  11. var compare = function(userChoice, computerChoice) {
  12.     if(userChoice === computerChoice) {
  13.         return "The result is a tie!";
  14.     }
  15.     else if (userChoice === "rock") {
  16.        
  17.         if(computerChoice === "scissors") {
  18.             return "rock wins";
  19.         }
  20.         else {
  21.             return "paper wins";
  22.         }
  23.     }
  24.     else if (userChoice === "paper") {
  25.    
  26.         if (computerChoice === "rock") {
  27.             return "paper wins";
  28.         }
  29.         else {
  30.             return "scissors wins";
  31.         }
  32.     }
  33.     else if(userChoice === "scissors") {
  34.         if (computerChoice === "rock") {
  35.             return "rock wins";
  36.         }
  37.         else {
  38.             return "scissors wins";
  39.         }
  40.     }
  41. }
  42. alert("The computer chooses " + computerChoice);
  43. alert (compare(userChoice,computerChoice));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement