I'm about to get final exam, this is the practice thing.
Please help me get throug it, i have got stuck here for a long time.
This is the question.
Look at the JavaScript program for this problem. It already contains a function called rollDie which returns a number between 1 and 6.
There are two buttons on the html document for player 1 and player 2 to click to roll a die.
When player one clicks the ‘Player 1 Roll’ button the onclick attribute should call the function playerOne which calls the rollDie function and stores the returned result in a variable.
When player two clicks the ‘Player 2 Roll’ button the onclick attribute should call the function playerTwo which calls the rollDie function and stores the returned result in a variable.
The two results should be displayed in the corresponding divs with id ‘playerOneResult’ and id ‘playerTwoResult’. Once both players have had a turn, the program should compare their results and display a message in another div with id ‘message’.
For example: If Player 1’s roll is a 3 and Player 2’s roll is a 4, the message displayed should be “Player Two wins”.
This is JS code
//Declare the variables
var playerOneNumber = 0;
var playerTwoNumber = 0;
//Functions
function rollDie()
{
//create a random integer between 1 and 6
var randomSide = Math.floor( Math.random() * 6 ) + 1;
return randomSide
}
function PlayerOne()
{
var playerOneNumber = rollDie();
return playerOneNumber;
}
var playerOneNumber = PlayerOne();
var playerOneR = document.getElementById("playerOneResult").innerHTML = playerOneNumber;
function PlayerTwo()
{
var playerTwoNumber = rollDie();
return playerTwoNumber;
}
var playerTwoNumber = PlayerTwo();
var playerTwoR = document.getElementById("playerTwoResult").innerHTML = playerTwoNumber;
if(playerOneNumber > playerTwoNumber)
{
document.getElementById("message").innerHTML = "Player One wins";
}
else if(playerOneNumber < playerTwoNumber)
{
document.getElementById("message").innerHTML = "Player Two wins";
}
This is HTML
<body>
<h2>Programming Project - Problem P</h2>
<input type="button" id="player1" onclick="PlayerOne();" value="Player 1 Roll">
<input type="button" id="player2" onclick="PlayerTwo();" value="Player 2 Roll">
<p>Player One rolled a <span id="playerOneResult">***</span></p>
<p>Player Two rolled a <span id="playerTwoResult">***</span></p>
<p>Result: <span id="message">***</span></p>
</body>