Nestalna
Nestalna

Reputation: 67

My generator is not working

Hi I am generating a list of restaurants in my town (because my husband and I can never decide on one) and it looks like the generator is only choosing between 0-7 (there is 39 total).

I don't see any problems thus far so I need an extra pair of eyes. What in the world am I doing wrong? Please help!

<html>
<head>
  
<title>Restaurant Generator</title>
<h1 style="font-family:helvetica;">Restaurant Generator</h1>
<script language type="text/javascript">
<!--

function getMessage()
{
var ar = new Array(40)
ar[0] = "Painted Horse"
ar[1] = "Indian Coffe Co"
ar[2] = "Taco Bell"
ar[3] = "Chinese by Food Pyramid"
ar[4] = "Cinese Buffet"
ar[5] = "Luigis"
ar[6] = "Dink's"
ar[7] = "La Fiesta"
ar[8] = "Taco Mayo"
ar[9] = "Papa Murphey's"
ar[10] = "Hideway"
ar[11] = "McAllister's"
ar[12] = "McDonalds"
ar[13] = "Jimmy Johns"
ar[14] = "Rolling Pin"
ar[15] = "Lot A Burger"
ar[16] = "Philips caf"
ar[17] = "OKWU caf"
ar[18] = "Chilli's"
ar[19] = "Bouldevard Dinner"
ar[20] = "Frank and Lola's"
ar[21] = "Garfields's"
ar[22] = "Subway"
ar[23] = "Sonic"
ar[24] = "Golden Corral"
ar[25] = "Buffalo Wild Wings"
ar[26] = "Sushi One"
ar[27] = "Eskimo Joe's"
ar[28] = "Eggbert's"
ar[29] = "Midway Caffee"
ar[30] = "Copper Bar"
ar[31] = "Billy Sims"
ar[32] = "Arby's"
ar[33] = "KFC"
ar[34] = "Hunan's"
ar[35] = "Braum's"
ar[36] = "A&W"
ar[37] = "Burger King"
ar[38] = "Guess again"
ar[39] = "Wallmart hot food"
// add as many more that you can stand but make
// sure you update the value '7' in the alert box
var now = new Date()
var sec = now.getSeconds()
alert("Today you are eating at:\n\n" + ar[sec % 7])
}

//-->
  </script>

</head>
<!-- <body onLoad="getMessage()"> this will automatically generate when you load the page-->
<body>
<form>
<input type="button" style="height:100px; width: 600px; font-size:50px" name="again" value="Click Me" onClick="getMessage()">
</form>

</body>
</html> 

Upvotes: 0

Views: 42

Answers (1)

Amadan
Amadan

Reputation: 198314

Change ar[sec % 7] into ar[sec % ar.length]. ("make sure you update the value '7' in the alert box" should have been a dead giveaway - 40 would work instead of 7, but ar.length makes updating not needed as it will work for any number of restaurants.)

BTW, not an error, but very unJavaScripty: the way you create your array. It should work as it is, but it is very easy to make a mistake. I suggest changing

var ar = new Array(40)
ar[0] = "Painted Horse"
ar[1] = "Indian Coffee Co"
//...

into

var ar = [
  "Painted Horse",
  "Indian Coffee Co",
  //...
];

Upvotes: 3

Related Questions