www.webdeveloper.com
Results 1 to 8 of 8

Thread: Accessing variables inside a function

  1. #1
    Join Date
    Jun 2006
    Location
    Doncaster
    Posts
    78

    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>

  2. #2
    Join Date
    Jun 2007
    Posts
    667
    There's a missing " in the <input> element.

  3. #3
    Join Date
    Jun 2006
    Location
    Doncaster
    Posts
    78
    Sterling work. Thanks!
    But that's not the problem...

  4. #4
    Join Date
    Jun 2007
    Posts
    667
    Quote Originally Posted by AliHurworth View Post
    Sterling work. Thanks!
    But that's not the problem...
    You also have an unterminated string in an alert.

    Use the error console

  5. #5
    Join Date
    Aug 2010
    Location
    Baltimore, Maryland, United States
    Posts
    138
    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); // 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>

  6. #6
    Join Date
    Jun 2006
    Location
    Doncaster
    Posts
    78
    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>

  7. #7
    Join Date
    Jun 2007
    Posts
    667
    Quote Originally Posted by AliHurworth View Post
    Hi both, thanks for sorting out the syntax.
    However, it still doesn't seem to work as planned...
    https://developer.mozilla.org/en/Cor...ison_Operators

  8. #8
    Join Date
    Jun 2006
    Location
    Doncaster
    Posts
    78

    Works...

    Sterling you are right: it was the = vs == thing. Full code to follow.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles