我的发电机不工作

时间:2014-10-16 00:50:12

标签: javascript random

嗨我正在制作一个我镇上的餐馆清单(因为我丈夫和我永远不能决定一个),看起来发电机只选择0-7(共有39个)。

到目前为止我没有看到任何问题所以我需要一双额外的眼睛。世界上我做错了什么?请帮忙!

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

1 个答案:

答案 0 :(得分:3)

ar[sec % 7]更改为ar[sec % ar.length]。 (&#34;请务必更新值&#39; 7&#39;在提醒框中&#34;应该是一个死的赠品 - 40将起作用而不是7,但{ {1}}不需要更新,因为它适用于任意数量的餐馆。)

BTW,不是错误,而是非常unJavaScripty:你创建数组的方式。它应该按原样运行,但很容易出错。我建议改变

ar.length

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