dcsimg
www.webdeveloper.com
Results 1 to 3 of 3

Thread: random number difficulty

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

    random number difficulty

    Hi all,
    I'm struggling to get this function to work on a page designed to solve a maths problem.
    Basically, the page creates a random 1 or 0, and shows an alert box with a different message for each, plus a running total counter.

    For some reason, it only shows the 1 text - and I can't see if this is a mistake with the randomnumber variable, of the if-else statement.

    Can anyone help?

    HTML 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">
    i=0;
    var box_ttl=80;
    var single;
    var dbl=80;
    var counter;
    function maketea()
    {
    i++;
    var randomnumber=Math.floor(Math.random()*2);
    if (randomnumber=0)
    	{
    		box_ttl=box_ttl-1;
    		alert('Try number '+i+'. A single. '+box_ttl+' left');
    	}
    else (randomnumber=1)
    	{
    		box_ttl=box_ttl-1;
    		alert('Try number '+i+'. A double. '+box_ttl+' left');
    		dbl=dbl-1;
    		single=single+1;
    	}
    }
    </script>
    </head>
    
    <body>
    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>.
    <hr />
    <INPUT TYPE="button" NAME="myButton" VALUE="Make the tea!" onClick="maketea()">
    </body>
    </html>
    Obviously, there is some more code to add...

  2. #2
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    You are actually assigning values to randomnumber in the 'if' and 'else' (which should be 'else if' in this case, 'else' should not have anything in parens () or any parens at all after it) statements by only using single = , when you should be using double equal ==
    Below would be the corrected version (with validation errors fixed too):

    HTML 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 type="text/javascript">
    i=0;
    var box_ttl=80;
    var single;
    var dbl=80;
    var counter;
    function maketea()
    {
    i++;
    var randomnumber=Math.floor(Math.random()*2);
    if (randomnumber==0)
    	{
    		box_ttl=box_ttl-1;
    		alert('Try number '+i+'. A single. '+box_ttl+' left');
    	}
    else if(randomnumber==1)
    	{
    		box_ttl=box_ttl-1;
    		alert('Try number '+i+'. A double. '+box_ttl+' left');
    		dbl=dbl-1;
    		single=single+1;
    	}
    }
    </script>
    </head>
    
    <body>
    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>.
    <hr />
    <form action="">
    <input type="button" name="myButton" value="Make the tea!" onclick="maketea()" />
    </form>
    </body>
    </html>
    Last edited by astupidname; 01-07-2009 at 03:57 AM.

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

    Yes of course...

    Works like a CHARM.
    It's been a while - I've probably forgotten more than I ever knew anyway!

    Thanks

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