Advertisement
Guest User

Untitled

a guest
May 26th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var getRandomInt = function (min, max)
  2. {
  3.   return Math.floor(Math.random() * (max - min)) + min;
  4. }
  5.  
  6. var compareChoice = function (choice1, choice2)
  7. {
  8.     if (compareValueChoice("Paper", "Rock", choice1, choice2))
  9.     {
  10.         return ("Paper wins");
  11.     }
  12.     else if (compareValueChoice("paper", "Scissors", choice1, choice2))
  13.     {
  14.         return ("Scissors wins");
  15.     }
  16.     else if (compareValueChoice("Scissors", "Rock", choice1, choice2))
  17.     {
  18.         return ("Rock wins");
  19.     }
  20.     return ("Tie");
  21. }
  22.    
  23. var compareValueChoice = function (value1, value2, choice1, choice2)
  24. {
  25.     if (value1 === choice1 && value2 === choice2)
  26.     {
  27.         return true;
  28.     }
  29.     else if (value1 === choice2 && value2 === choice1)
  30.     {
  31.         return true;
  32.     }
  33.     return false;
  34. }
  35.  
  36. var playGame = function ()
  37. {
  38.     var userChoice = ""
  39.     while (userChoice != "Rock" || userChoice != "Paper" || userChoice != "Scissors")
  40.     {
  41.         userChoice =  prompt("Do you choose Rock, Paper or Scissors?");
  42.     }
  43.     console.log("You chose " + userChoice);
  44.    
  45.     var aiChoice = ""
  46.     switch(getRandomInt(1, 4))
  47.     {
  48.         Case 1:
  49.             aiChoice = "Rock";
  50.             break;
  51.         Case 2:
  52.             aiChoice = "Paper";
  53.             break;
  54.         case 3:
  55.             aiChoice = "Scissors";
  56.             break;
  57.     }
  58.     console.log("Computer chose " + aiChoice);
  59.    
  60.     return compareChoice(userChoice, aiChoice)
  61. }
  62.  
  63.  var result = ""
  64.  do
  65.  {
  66.      result = playGame()     
  67.  } while (result == "Tie")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement