...

View Full Version : simple random array



the oso
10-13-2008, 11:57 PM
I'm just trying to get this code to randomly select a restaurant to eat at when the button is clicked. I'm having trouble doing the random part. here is the code I got:


<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Random Restaraunt Selector</title>
<script type="text/javascript">
//PTE = Place to eat
<!--
function getpte(ptenum) {
return Math.floor(Math.random()* ptenum);
}
//-->
</script>
</head>
<body>
<h2>Random Restaraunt Selector</h2>
<script type="text/javascript">

<!--
var pte = new Array("Jimmy John's","Momo's","Subway", "Pita Pit", "Fresh Stirfry", "Cici's", "Quiznos", "Hungry Howies", "Decent Pizza", "Stevi B's", "Wendy's", "Super Perros");

for (x in pte)
{
document.write(pte[x] + "<br />");
}
//-->

getpte = ptenum(12); // 12 values, 0 - 11
document.write("The Winner is... ");
document.write(ptenum);

</script>
<form>
<p></p>
<p><input type="button" value="Click me!" onclick="getpte(this.form.pte.value)" /></p>
</form>
</body>
</html>

jmrker
10-14-2008, 02:57 AM
Note differences from your script ...


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Random Restaraunt Selector</title>
<script type="text/javascript">
//PTE = Place to eat

function getpte(ptenum) { return Math.floor(Math.random()* ptenum); }

var pte = new Array("Jimmy John's","Momo's","Subway", "Pita Pit", "Fresh Stirfry", "Cici's", "Quiznos",
"Hungry Howies", "Decent Pizza", "Stevi B's", "Wendy's", "Super Perros");
function ShowList() { for (x in pte) { document.write(pte[x] + "<br />"); } }

function PickWinner() {
var ptenum = getpte(pte.length); // 12 values, 0 - 11
var str = "The Winner is ... " + pte[ptenum];
document.getElementById('winner').innerHTML = str;
}
</script>
</head>
<body>
<h2>Random Restaraunt Selector</h2>
<script type="text/javascript"> ShowList(); </script>
<p></p>
<p><input type="button" value="Click me!" onclick="PickWinner();" /></p>
<p>
<div id="winner" style="font-size:50px;color:red"></div>
</body>
</html>

the oso
10-16-2008, 02:23 PM
thanks man, I appreciate it.

jmrker
10-16-2008, 04:24 PM
You're most welcome.
If you understand the differences then I'm glad I was able to help.
Good Luck!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum