SHARE
TWEET

Untitled

a guest Apr 24th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Initialize user and computer scores, set to 0
  2. let userScore = 0;
  3. let compScore = 0;
  4.  
  5. // Store DOM nodes that will be modified
  6. let elUserScore = document.getElementById('userScore');
  7. let elCompScore = document.getElementById('compScore');
  8. let elUserPick = document.getElementById('userPick');
  9. let elCompPick = document.getElementById('compPick');
  10. let elOverallResult = document.getElementById('overallResult');
  11.  
  12. // Set up function to compare the user and computer choice.
  13. let compare = function(choice1, choice2) {
  14.     if (choice1 === choice2) {
  15.         console.log("It's a Tie.");
  16.         return result = ["The result is a tie!", "tie"];
  17.     } else if (choice1 === "rock") {
  18.         if (choice2 === "scissors") {
  19.             console.log("rock beats scissors");
  20.             return result = ["rock wins", "user"];
  21.         } else if (choice2 === "paper") {
  22.             console.log("paper beats rock");
  23.             return result = ["paper wins", "comp"];
  24.         }
  25.     } else if (choice1 === "paper") {
  26.         if (choice2 === "rock") {
  27.             console.log("paper beats rock");
  28.             return result = ["paper wins", "user"];
  29.         } else if (choice2 === "scissors") {
  30.             console.log("scissors beats paper");
  31.             return result = ["scissors win", "comp"];
  32.         }
  33.     } else if (choice1 === "scissors") {
  34.         if (choice2 === "rock") {
  35.             console.log("rock beats scissors");
  36.             return result = ["rock wins", "comp"];
  37.         } else if (choice2 === "paper") {
  38.             console.log("scissors beats paper");
  39.             return result = ["scissors win", "user"];
  40.         }
  41.     }
  42. }
  43.  
  44. // Set up function that will allow the computer to make choice
  45. let computerRoll = function() {
  46.     var computerChoice = Math.random();
  47.     if (computerChoice < 0.34) {
  48.         var computerChoice = "rock";
  49.     } else if(computerChoice <= 0.67) {
  50.         var computerChoice = "paper";
  51.     } else {
  52.         var computerChoice = "scissors";
  53.     }
  54.     console.log("Computer Choice is " + computerChoice);
  55.     return computerChoice;
  56. };
  57.  
  58. let userChoice = function(choice) {
  59.     var userChoice = choice;
  60.     console.log("User Choice is " + choice);
  61.     var computerChoice = computerRoll();
  62.     console.log(compare(userChoice, computerChoice));
  63.     console.log(result[0]);
  64.     // getResult increments either user or computer scores
  65.     const getResult = (function() {
  66.         if (result[1] === "user") {
  67.             userScore++;
  68.             return userScore;
  69.         } else if (result[1] === "comp") {
  70.             compScore++;
  71.             return compScore;
  72.         } else if (result[1] === "tie") {
  73.         }
  74.     } () );
  75.     // Modify elements to new values
  76.     elUserScore.textContent = userScore;
  77.     elCompScore.textContent = compScore;
  78.     elUserPick.textContent = userChoice;
  79.     elCompPick.textContent = computerChoice;
  80.     elOverallResult.textContent = result[0];
  81. }
  82.  
  83. // Event listeners call userChoice with string value
  84. let rockBox = document.getElementById('rock');
  85. rockBox.addEventListener('click', function() {
  86.     userChoice("rock");
  87. }, false);
  88.  
  89. let paperBox = document.getElementById('paper');
  90. paperBox.addEventListener('click', function() {
  91.     userChoice("paper");
  92. }, false);
  93. let scissorBox = document.getElementById('scissors');
  94. scissorBox.addEventListener('click', function() {
  95.     userChoice("scissors");
  96. }, false);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top