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