d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Code Not Working
Add Reply New Topic New Poll
Member
Posts: 467
Joined: Nov 18 2013
Gold: 0.00
Nov 18 2013 11:11pm
Does anyone know why this code doesn't work when i try to count the number of sevens?
Code
<!doctype html>
<!Lab 5>

<html>
<head>
<title> Die Rolls </title>
<script type="text/javascript" src="http://balance3e.com/random.js">
</script>
<script type="text/javascript">


function ResetCounters()
{
document.getElementById('rollSpan').innerHTML = 0
document.getElementById('doubleSpan').innerHTML = 0
}

function RollDice()
// Assumes: die images are in http://balance3e.com/Images
// Results: displays 2 random die rolls & keeps a count in rollSpan
{
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;

if (roll1 == roll2)
{
document.getElementById('doubleSpan').innerHTML =
parseFloat(document.getElementById('doubleSpan').innerHTML) + 1;

}

if (roll1 == 1 && roll2 == 6)
{
document.getElementById(‘sevenSpan').innerHTML =
parseFloat(document.getElementById(‘sevenSpan').innerHTML) + 1;

}



}


</script>
</head>

<body>
<div style="text-align:center">
<p>
<img id="die1Img" alt="die image"
src="http://balance3e.com/Images/die1.gif">
<img id="die2Img" alt="die image"
src="http://balance3e.com/Images/die1.gif">
</p>
<input type="button" value="Click to Roll" onclick="RollDice();">
<input type="button" value="Reset" onclick="ResetCounters();">
<hr>
<p>
Number of rolls: <span id="rollSpan">0</span> <br>
Number of doubles: <span id="doubleSpan">0</span> <br>
Number of sevens: <span id=“sevenSpan”>0</span>
</p>
</div>
</body>
</html>
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 18 2013 11:25pm
look into firebug or chrome developer tools
Member
Posts: 57
Joined: Oct 21 2013
Gold: 0.00
Nov 19 2013 04:49pm
Maybe instead of two if statements you use a else if for the second if, and else statement stating bool = false and make a condition statement in the first if loop to if(roll1 == roll2 & bool = true) (since bool will always be = true when you define it in the beginning. Or try defining the variables outside of the function as global variables and plug them in to the function, function RollDice(roll1, roll2) { }. Just a guess and worth a try.
Member
Posts: 17,533
Joined: Oct 16 2010
Gold: Locked
Trader: Scammer
Nov 19 2013 09:54pm
Quote (alex015110 @ Nov 19 2013 02:49pm)
Maybe instead of two if statements you use a else if for the second if, and else statement stating bool = false and make a condition statement in the first if loop to if(roll1 == roll2 & bool = true) (since bool will always be = true when you define it in the beginning. Or try defining the variables outside of the function as global variables and plug them in to the function, function RollDice(roll1, roll2) { }. Just a guess and worth a try.


This.

Your strings are nice, but sometimes you have to cut it into sections.

Go Back To Programming & Development Topic List
Add Reply New Topic New Poll