Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>Rock, Paper and Scissors</title>
- </head>
- <body>
- <button class="option" name="rock" value="rock">Rock</button>
- <button class="option" name="paper" value="paper">Paper</button>
- <button class="option" name="scissors" value="scissors">Scissors</button>
- </body>
- </html>
- <script type="text/javascript">
- var btn = document.getElementsByClassName("option");
- for(i=0; i < btn.length; i++){
- btn[i].onclick = function() {
- var user_choice = btn.value;
- var items = new Array("rock","paper","scissors");
- var computer_choice = items[Math.floor(Math.random()*items.length)];
- switch(user_choice){
- case "rock":
- if(computer_choice == "rock"){
- document.write("It's a tie!");
- }
- else if (computer_choice == "paper") {
- document.write("Computer wins with paper!");
- }
- else {
- document.write("Computer loses with scissors!");
- }
- break;
- case "paper":
- if(computer_choice == "rock"){
- document.write("Computer loses with rock!");
- }
- else if (computer_choice == "paper") {
- document.write("It's a tie!");
- }
- else {
- document.write("Computer wins with scissors!");
- }
- break;
- case "scissors":
- if(computer_choice == "rock"){
- document.write("Computer wins with rock!");
- }
- else if (computer_choice == "paper") {
- document.write("Computer loses with paper!");
- }
- else {
- document.write("It's a tie!");
- }
- break;
- }
- }
- }
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement