Results 1 to 3 of 3

Thread: Math makes my head hurt, anyway. =/

  1. #1
    Join Date
    Jun 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Math makes my head hurt, anyway. =/

    Greetings folks, I hope you're having a good day.

    I've recently added some silly musings to my site on page load, I did this with PHP.. unfortunately they were such a hit that everybody just sits there refreshing the main page over and over for hours on end (literally) to see the next message. As you can imagine, even with cookies this is quite volatile on the bandwidth, so I searched for a way to only be able to reload a div, or rather hopefully a <p>aragraph. -- Google tells me Javascript is my savior!

    So I'm trying (failing) my hand at Javascript~ I found a script on the internetter-ma-bob that for the most part does the trick, and I actually got to work in any semblance (99% sure I'm just an idiot), but I was hoping one of you could assist me in tinkering it to do one little thing.

    It loads a random message on page load, and with a button you can change the message it says without reloading the entire page. Unfortunately, when you press the button, it just picks the next message in the list. I'd like it to pick a random message from the list every time the button is clicked.

    Code:
    var Statements = new Array(
    
      'I like turtles.'
      'I like tortoises.'
      'I like terrapins.'
    
    );
    
    
    
    function GetStatement(outputtype)
    {
    	if(++Number > Statements.length) Number = 0;
    	if (outputtype==0)
    	document.write(Statements[Number])
    	else if (document.getElementById)
    	document.getElementById("ran-dumb").innerHTML=Statements[Number];
    }
    
    
    
    function GetRandomNumber(lbound, ubound) 
    {
    	return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
    }
    
    
    var Number = GetRandomNumber(0, Statements.length);
    Code:
    <script src="ran-dumb.js" language="javascript" type="text/javascript"></script>
    
    <p id="ran-dumb">
    <script type="text/javascript">
    GetStatement(0)
    </script>
    </p>
    
    <p>
    <button style="background-color:#220C19;color:#DB44DB;" onClick="GetStatement(1)">New Quote!</button>
    </p>

    Thank you in advance for your time and any assistance you may give,
    ~FailureAtJavascript
    Last edited by FailureAtJavascript; 06-08-2010 at 05:36 AM. Reason: Resolved!

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    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" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script type="text/javascript">
    /*<![CDATA[*/
    var Statements =[
    
      'I like turtles.',
      'I like tortoises.',
      'I like terrapins.'
    
    ];
    
    
    
    function GetStatement(){
        var Number=Math.floor(Math.random()*Statements.length);
        if (Number!=this.nu){
        	document.getElementById("ran-dumb").innerHTML=Statements[Number];
            this.nu=Number;
        }
        else {
         GetStatement();
        }
    }
    
    
    
    /*]]>*/
    </script></head>
    
    <body>
    <p id="ran-dumb">
    <script type="text/javascript">
    GetStatement(0)
    </script>
    </p>
    
    <p>
    <button style="background-color:#220C19;color:#DB44DB;" onClick="GetStatement(1)">New Quote!</button>
    </p>
    </body>
    
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Jun 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Much appreciated good sir!~ This is exactly what I was hoping for.

    You are a prince among Javascript coders. =P

Bookmarks

Posting Permissions

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