Last active
September 18, 2018 11:07
-
-
Save Nerajno/eff1ee87b63a6c45bdc406bf07ad6b76 to your computer and use it in GitHub Desktop.
rock paper scissors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<title>Rock-Paper_Scissors</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<!-- <link rel="stylesheet" type="text/css" media="screen" href="main.css" /> | |
<script src="main.js"></script> --> | |
</head> | |
<body> | |
<h1>Rock, Paper, and Scissors</h1> | |
<h2>Computer: <span id="computer-move"></span></h2> | |
<h2>You: <span id="player-move"></span></h2> | |
<h2></h2> | |
<h2 id="game-result"></h2> | |
<label>Your Move:</label> | |
<button id="play-rock">Rock</button> | |
<button id="play-paper">Paper</button> | |
<button id="play-scissors">Scissors</button> | |
</body> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> | |
</script> | |
<script> | |
// Write a determineWinner function which : | |
// a. determines who the winner is, or if it is a tie | |
// b. updates the text in the id="game-result" h2 element to say either: | |
// "YOU WIN!", "YOU LOSE!" or "It's a tie." | |
function determineWinner(inputOne, inputTwo) { | |
var player = "You"; | |
var computer = "Computer"; | |
if (inputOne === "Rock" && inputTwo === "Scissors") { | |
$("#game-result").text(player + " wins"); | |
} else if (inputOne === "Paper" && inputTwo === "Scissors") { | |
$("#game-result").text(computer + " wins"); | |
} else if (inputOne === "Scissors" && inputTwo === "Rock") { | |
$("#game-result").text(computer + " wins"); | |
} else if (inputOne === "Scissors" && inputTwo === "Paper") { | |
$("#game-result").text(player + " wins"); | |
} else if (inputOne === "Paper" && inputTwo === "Rock") { | |
$("#game-result").text(player + " wins"); | |
} else if (inputOne === "Rock" && inputTwo === "Paper") { | |
$("#game-result").text(computer + " wins"); | |
} else if (inputOne === inputTwo) { | |
$("#game-result").text("Game drawn") | |
} else { | |
$("#game-result").text("This is broken"); | |
} | |
} | |
// NOTE: could have used switch statements... will refactor with them if needed | |
// Write a playComputerMove function, which randomly sets the text in the id="computer-move" | |
// span element to either "Rock", "Paper", or "Scissors" | |
// NOTE: This is not clear..... is it on.click or pg load | |
function playComputerMove() { | |
var btnIds = ["play-rock", "play-paper", "play-scissors"] | |
var playSelector = Math.floor(Math.random() * btnIds.length); | |
playSelector = $("#" + btnIds[playSelector]).text() | |
return playSelector; | |
} | |
playComputerMove(); | |
// When the "Rock" button is clicked, put the text "Rock" in the id="player-move" span element. | |
// Do the same as above for the "Paper" and "Scissors" buttons. | |
$("button").on("click", function() { | |
var clickdbtn = $(this).text(); | |
var playerMove = $("#player-move").text(clickdbtn); | |
var computerMove = $("#computer-move").text(playComputerMove); | |
var test = computerMove.text(); //computer move | |
var test2 = playerMove.text(); //player move | |
determineWinner(test, test2); | |
// var test = computerMove[""0""].innerHTML; | |
// console.log(test,",",test2); | |
}) | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment