If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Hybrid View

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:

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>

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>

<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>