Accessing variables inside a function
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>