View Single Post
Old 10-09-2012, 03:27 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,032
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Code:
<!DOCTYPE html>
<head>
</head>
<body>
    
<p id="generator">Generate a random fact</p>

<button onclick="myFunction()">Try it</button>

<div id="boldStuff"">
</div>

<script type = "text/javascript">

function myFunction() {
var r_text = [];
r_text[0] = "Give a homeless man a gift card";
r_text[1] = "Hold the door for a stranger";
r_text[2] = "Pay for a stranger's lunch";
r_text[3] = "Compliment the next person you see";
r_text[4] = "Take your neighbor's dog for a walk";
r_text[5] = "Send greeting cards to random addresses";
r_text[6] = "Load a vending machine with money";
var i = Math.floor(7*Math.random())

document.getElementById('boldStuff').innerHTML = r_text[i].toUpperCase();
}

</script>
</div>

</body>
</html>
A small defect is that the same quote may appear twice in succession by random selection. You can overcome this by shuffling your array.

Your uppercase function is quite superflous here.

<script language=javascript> is long deprecated. Use <script type = "text/javascript"> instead (in fact also deprecated but still necessary for IE<9).
The <!-- and //--> comment (hiding) tags have not been necessary since IE3 (i.e. since September 1997). If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.


Quizmaster: Which African country gives its name to the complaint known as 'gippy tummy'?
Contestant: Venezuela.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
cmyk529 (10-18-2012)