www.webdeveloper.com
Results 1 to 4 of 4

Thread: Random Quotes in h2 tag Math.Random()

  1. #1
    Join Date
    Dec 2011
    Posts
    1

    Post Random Quotes in h2 tag Math.Random()

    Hi forum,
    I am new to javascript but have been using java for quite a while.
    I am looking to make a random quote (out of ten possible quotes) appear in my h2 tag in a page i am working on. In java, i would make a random number generator, in javascript it looks like this:
    Code:
    var randomnumber=Math.floor(Math.random()*11)
    Then make an if statement:
    Code:
    if randomnumber=1 {
    var quote="Live long and prosper"
    }
    document.write(var quote);
    Could someone more experienced than me tell me if my code looks good and how would a go about getting "var quote" in my h2 tag?
    Thanks in advance.

  2. #2
    Join Date
    Nov 2010
    Posts
    978
    it's OK, although the if statement would look a lot better like this:
    Code:
    if (randomnumber==1) {...}
    But it's going about it the long way. have a look at the following (and thanks to bionoid for the quotes array):

    Code:
    <html>
    <head>
    </head>
    <body>
    <h2 id="text"><h2>
    <script type="text/javascript">
    var randomnumber=Math.floor(Math.random()*11)
    quotes = [
    				'C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg.',
    				'Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.',
    				'A computer lets you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila.',
    				'If debugging is the process of removing software bugs, then programming must be the process of putting them in.',
    				'There are two major products that come out of Berkeley: LSD and UNIX. We don\'t believe this to be a coincidence.',
    				'There are two ways to write error-free programs; only the third one works.',
    				'It\'s not a bug - it\'s an undocumented feature',
    				'A good programmer is someone who always looks both ways before crossing a one-way street.',
    				'The trouble with programmers is that you can never tell what a programmer is doing until it\'s too late.',
    				'Don\'t worry if it doesn\'t work right. If everything did, you\'d be out of a job'
    			];
    document.getElementById("text").innerHTML=quotes[randomnumber];
    			</script> 
    
    </body>
    </html>
    Last edited by xelawho; 12-14-2011 at 11:49 AM.

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,263

    Lightbulb

    I would recommend one minor change to allow you to add or subtract from the
    quotes array without recalculating the number of elements (11 in the original).
    Code:
    <html>
    <head>
    </head>
    <body>
    <h2 id="text"><h2>
    <script type="text/javascript">
    var quotes = [
      'C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg.',
      'Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.',
      'A computer lets you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila.',
      'If debugging is the process of removing software bugs, then programming must be the process of putting them in.',
      'There are two major products that come out of Berkeley: LSD and UNIX. We don\'t believe this to be a coincidence.',
      'There are two ways to write error-free programs; only the third one works.',
      'It\'s not a bug - it\'s an undocumented feature',
      'A good programmer is someone who always looks both ways before crossing a one-way street.',
      'The trouble with programmers is that you can never tell what a programmer is doing until it\'s too late.',
      'Don\'t worry if it doesn\'t work right. If everything did, you\'d be out of a job'
    ];
    
    var randomnumber=Math.floor(Math.random()*quotes.length)
    document.getElementById("text").innerHTML=quotes[randomnumber];
    			</script> 
    
    </body>
    </html>

  4. #4
    Join Date
    Nov 2010
    Posts
    978
    and then there's the we-hate-gloabal-variables approach...

    Code:
    <html>
    <head>
    </head>
    <body>
    <h2 id="text"><h2>
    <script type="text/javascript">
    
    var quotes = [
    				"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg.",
    				"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.",
    				"A computer lets you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila.",
    				"If debugging is the process of removing software bugs, then programming must be the process of putting them in.",
    				"There are two major products that come out of Berkeley: LSD and UNIX. We don't believe this to be a coincidence.",
    				"There are two ways to write error-free programs; only the third one works.",
    				"It's not a bug - it's an undocumented feature",
    				"A good programmer is someone who always looks both ways before crossing a one-way street.",
    				"The trouble with programmers is that you can never tell what a programmer is doing until it's too late.",
    				"Don't worry if it doesn't work right. If everything did, you'd be out of a job"
    			];
    			document.getElementById("text").innerHTML=quotes[Math.floor(Math.random()*quotes.length)];
    			</script> 
    
    </body>
    </html>

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