Hi, I've attached a cut-down version of a script I am working on. It's a pretty simple button with a function attached, which creates a random number and compares it with a preset value.
I can't seem to get the function to read the variables!
Can anyone point me in the right direction?
Code:
#<html header>
<script language="javascript" type="text/javascript">
var firstrun=0;
var a=80;
var b=0;
var rdm=0;
var total=a + b;
function calc() {
alert('firstrun: ' + firstrun);
alert('total at start of function: ' + total);
if (firstrun=0) {
firstrun=1;
}
alert ('firstrun after if: ' + firstrun)
//create a random number, multiply by 100
alert ('total before random: ' + total);
rdm=((Math.random())*total);
//round this number to a whole number (integer)
rdm1=Math.round(rdm);
alert ('final random number: ' + rdm1);
alert('final total number: ' + total);
if (rdm<sgl) {
alert('the random number was LESS than a')
} else {
alert('the random number was MORE than the a)
};
}
</script>
<body>
<INPUT TYPE="button" NAME="myButton" VALUE="Calculate onClick="calc()">
</body>
</html>
#<html header>
<script language="javascript" type="text/javascript">
var firstrun=0;
var a=80;
var b=0;
var rdm=0;
var total=a + b;
function calc() {
alert('firstrun: ' + firstrun);
alert('total at start of function: ' + total);
if (firstrun=0) {
firstrun=1;
}
alert ('firstrun after if: ' + firstrun); // semi-colon
//create a random number, multiply by 100
alert ('total before random: ' + total);
rdm=((Math.random())*total);
//round this number to a whole number (integer)
rdm1=Math.round(rdm);
alert ('final random number: ' + rdm1);
alert('final total number: ' + total);
if (rdm<sgl) {
alert('the random number was LESS than a'); // semi-colon
} else {
alert('the random number was MORE than the a'); // end string, semi-colon
};
}
</script>
<body>
Hi both, thanks for sorting out the syntax.
However, it still doesn't seem to work as planned...
This is the full code, including your adjustments (don't worry about the teabags thing!):
Code:
<!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">
//initial state: declare variables
//counter
var i=0;
var firstrun=0;
var dbl=80;
var sgl=0;
var cups=0;
var rdm=0;
var total=dbl+sgl;
//create the random number
function brew() {
alert('firstrun: ' + firstrun);
alert('total at start of function: ' + total);
if (total=0) {
alert ('You have run out of teabags!');
} else {
alert ('You still have teabags!');} ;
if (firstrun=0) {
dbl=80;
firstrun=1;
alert ('firstrun after if: ' + firstrun);
}
//create a random number, multiply by 100
alert ('total before random: ' + total);
rdm=(((Math.random())*total)*100);
//round this number to a whole number (integer)
rdm1=Math.round(rdm);
alert ('final random number: ' + rdm1);
alert('final total number: ' + total);
if (rdm<sgl) {
alert('the random number was LESS than the singles');
} else {
alert('the random number was MORE than the singles') ;
};
// get current totals
//update totals
//display
}
</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="brew()">
<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>
Bookmarks