Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Rock, Paper and Scissors</title>
  5. </head>
  6. <body>
  7.     <button class="option" name="rock" value="rock">Rock</button>
  8.     <button class="option" name="paper" value="paper">Paper</button>
  9.     <button class="option" name="scissors" value="scissors">Scissors</button>  
  10. </body>
  11. </html>
  12. <script type="text/javascript">
  13.     var btn = document.getElementsByClassName("option");
  14.     for(i=0; i < btn.length; i++){
  15.         btn[i].onclick = function() {
  16.             var user_choice = btn.value;
  17.             var items = new Array("rock","paper","scissors");
  18.             var computer_choice = items[Math.floor(Math.random()*items.length)];
  19.             switch(user_choice){
  20.                 case "rock":
  21.                     if(computer_choice == "rock"){
  22.                         document.write("It's a tie!");
  23.                     }
  24.                     else if (computer_choice == "paper") {
  25.                         document.write("Computer wins with paper!");
  26.                     }
  27.                     else {
  28.                         document.write("Computer loses with scissors!");
  29.                     }
  30.                     break;
  31.                 case "paper":
  32.                     if(computer_choice == "rock"){
  33.                         document.write("Computer loses with rock!");
  34.                     }
  35.                     else if (computer_choice == "paper") {
  36.                         document.write("It's a tie!");
  37.                     }
  38.                     else {
  39.                         document.write("Computer wins with scissors!");
  40.                     }
  41.                     break;
  42.                 case "scissors":
  43.                     if(computer_choice == "rock"){
  44.                         document.write("Computer wins with rock!");
  45.                     }
  46.                     else if (computer_choice == "paper") {
  47.                         document.write("Computer loses with paper!");
  48.                     }
  49.                     else {
  50.                         document.write("It's a tie!");
  51.                     }
  52.                     break;
  53.             }
  54.         }
  55.     }
  56.    
  57. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement