Click here to Skip to main content
15,917,702 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HTML
<!doctype html>
<!-- stats.html                                     Kaitlin -->
<!-- This page simulates dice rolls and keeps a roll count. -->
<!-- ====================================================== -->



<title> Die Rolls 



function RollDice()
// Assumes: die images are in http://balance3e.com/Images
// Results: displays 2 random die rolls & keeps a count in rollSpan ; checks for identical rolls & keeps count in doubleCount
{
var roll1, roll2;

roll1 = RandomInt(1, 6);
roll2 = RandomInt(1, 6);

document.getElementById('die1Img').src =
'http://balance3e.com/Images/die' + roll1 + '.gif';
document.getElementById('die2Img').src =
'http://balance3e.com/Images/die' + roll2 + '.gif';

document.getElementById('rollSpan').innerHTML =
parseFloat(document.getElementById('rollSpan').innerHTML) + 1;

rollSpan = 0;

doubleCount = 0;

if (roll1 == roll2){
doubleCount = doubleCount + 1
}

document.getElementById('doubleCount').innerHTML =
parseFloat(document.getElementById('doubleCount').innerHTML) + 1;
}




<div style="text-align: center">
<p>


</p>

<hr>
<p>
Number of rolls: <span id="rollSpan">0</span>
Number of doubles: <span id="doubleCount">0</span>
</p>
</div>


What I have tried:

This is what I have for my code so far. I am seeking help because the number of rolls and number of doubles appear the same at the bottom rather than the number of rolls increasing each time and number of doubles increasing only when doubles are rolled. I believe my if statement is off. Thanks in advance for any help!
Posted
Updated 29-Jul-17 4:12am
v3

1 solution

Problems that are obvious:

1. These variables are inside the function:
rollSpan = 0;
doubleCount = 0;
As a result, they will always be initialized to 0 whenever the function is called

2. Because of point 1 and there is no increment for rollSpan, it will always be zero.

3. Because of point 1, doubleCount will either be 0 or 1.

4. The code to display rollSpan and doubleCount made no sense.

5. Do you have a function called RandomInt?

Solutions to the various points:
1. Move the rollSpan=0 and doubleCount=0 outside of the function.

2. add rollSpan++ inside the function.

4. display the result of the variable, e.g.
document.getElementById('doubleCount').innerHTML = doubleCount;
5. Only you know.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900