I am very new to JavaScript and HTML. I am trying to get a 2 player rock, paper, scissors game where the first player chooses an option and then afterwards the second player will choose an option. I am having troubles linking the player1 and player2 together to enable me to use if and else statements effectively. I am not sure if I am using functions correctly either.
This is what I’ve done so far…
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width", intial-scale="1.0"/>
<title> Two Player Rock, Paper, Scissors</title>
<link href="style.css" rel="stylesheet">
</head>
<body>
<h1>2 Player Rock, Paper, Scissors</h1>
<div id="playerChoose">Player 1 Please Choose: </div>
<div class="player1Choices">
<button id="rock" onclick="playGame('Rock')">👊</button>
<button id="paper" onclick="playGame('Paper')">✋</button>
<button id="scissors" onclick="playGame('Scissors')">✌</button>
</div>
<div id=player2Choose>Player 2 Please Choose: </div>
<div class="player2Choices">
<button id="rock2" onclick="play2Game('Rock')">👊</button>
<button id="paper2" onclick="play2Game('Paper')">✋</button>
<button id="scissors2" onclick="play2Game('Scissors')">✌</button>
</div>
<div id="player1Display">Player 1: </div>
<div id="player2Display">Player 2: </div>
<div id="resultsDisplay"</div>
<script src="script.js"></script>
</body>
</html>
JavaScript
let player1Display = document.getElementById("player1Display");
let player2Display = document.getElementById("player2Display");
function playGame(player1Choice){
player1Display.textContent = (`Player 1: ${player1Choice}`);
}
function play2Game(player2Choice){
player2Display.textContent = (`Player 2: ${player2Choice}`);
}
2
Answers
One issue is within your html file:
you have not closed the opening tag.
The other issue that when you get the choices of the player, you will not be able to access them outside the function and so you will not be able to get the winner. I have the following code for your index.html:
You can use a global variable
result
to store the result.