[英]Problem with randomly-generated letters from Array
Sometimes the randomly-generated letter equals to 'undefined' 有时,随机生成的字母等于“ undefined”
Anyone know why? 谁知道为什么?
Code: 码:
<script type="text/javascript">
var myArray= ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
randomnumber=Math.floor(Math.random()*27);
alert(myArray[randomnumber]);
Well, it could have something to do with your Math.floor(Math.random()*27) using the number 27 when there are 26 letters in the English alphabet. 好吧,这可能与您的Math.floor(Math.random()* 27)使用数字27(英语字母中有26个字母)有关。
math random returns 0-.999...
math random * 27 returns 0-26.999...
floor math random * 27 returns 0 - 26
an array with every english letter has 26 items
which means it has items indexed from 0-25
so you get a blank in each box 1/27 of all loads. 因此您在每个箱子的所有负载的1/27中有一个空白。 However, once "#next" has been seeded it will always stay a "letter" because you're replacing it with
但是,一旦播种了“ #next”,它将始终保持“字母”,因为您将其替换为
$("#next").text(good[randomnumber]);
When randomnumber=27
good[randomnumber]=undefined
and $("#next").text(undefined)
does nothing so the contents of "#next" remain unchanged. 当
randomnumber=27
good[randomnumber]=undefined
并且$("#next").text(undefined)
不执行任何操作时, $("#next").text(undefined)
的内容保持不变。 Still broken, less obvious. 仍然破碎,不太明显。 Just change 27 to 26 and you're done.
只需将27更改为26,就可以完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.