hi all.
i'm struggling with the following code - can't seem to get the variables within the function to work...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Teabag calculator</title>
<script language="javascript" type="text/javascript">
var single=0;
var dbl=80;
var total=single + dbl;
alert (total);
var i=0;
function maketea()
{
alert ("total: " + total);
//button is pressed.
/*
The variables are: singles
doubles
singles + doubles
*/
//Create a random number, multiply up so it matches the total
var randomnumber=(Math.floor(Math.random()*total));
alert (randomnumber);
//set the counter
i=++i;
//Test
if (randomnumber>single)
//low, ie single
/*if a single bag is pulled, the doubles are unaffected, the singles fall by one
the total number falls by one, and the number of tries increases by one*/
{
single=--single;
//if (single<0){single++};
alert ("more:Singles left:" + single);
alert ("more:Doubles left:" + dbl);
total=dbl+single;
document.getElementById('tries').innerHTML=i;
document.getElementById('doubles').innerHTML=dbl;
document.getElementById('singles').innerHTML=single;
document.getElementById('total').innerHTML=total;
document.getElementById('doubles').style.color="black";
document.getElementById('singles').style.color="red"
}
else //(randomnumber==1)
//high, ie double
/*if a double is pulled, the doubles fall by one, the singles increase by one,
thetotal number left stays the same*/
{
dbl=--dbl;
//if (dbl<1) {dbl=0};
single=++single;
alert ("Greater: Singles left:" + single);
alert ("Greater: Doubles left:" + dbl);
total=dbl+single;
document.getElementById('tries').innerHTML=i;
document.getElementById('doubles').innerHTML=dbl;
document.getElementById('singles').innerHTML=single;
document.getElementById('total').innerHTML=total;
document.getElementById('singles').style.color="black";
document.getElementById('doubles').style.color="red";
}
}
if ((dbl=0)&&(single=0)){
alert('You have run out of tea!!')
};;
</script>
<style type="text/css">
<!--
.style1 {font-size: large}
.style2 {
font-family: "Trebuchet MS";
background-color: #FF9933;
text-align: center;
}
-->
</style>
</head>
<body class="style2">
<div>
This page simulates the "Dad problem", as illustrated on <a href="http://answers.yahoo.com/question/index;_ylt=AvyknayS8m5RMuoYpmoj_lPsy6IX;_ylv=3?qid=20090106022211AAr5W7Q">Yahoo! Answers</a>.
</div>
<div>
<center>
<INPUT TYPE="button" NAME="myButton" VALUE="Make the tea!" onClick="maketea()">
<table border="1" cellpadding="0" cellspacing="0" bordercolor="#CC9900" bgcolor="#FF9933">
<caption align="top">
<span class="style1">In the box</span>
</caption>
<tr>
<td width="60"><strong>Number of cups made</strong></td>
<td width="60"><strong>Doubles left</strong></td>
<td width="60"><strong>Singles left</strong></td>
<td width="60"><strong>Total (1's & 2's) in the box</strong></td>
</tr>
<tr>
<td class="style1" id="tries">0 </td>
<td class="style1" id="doubles">80</td>
<td class="style1" id="singles">0</td>
<td class="style1" id="total">80</td>
</tr>
</table>
</div>
</body>
</html>