我创建一个随机事实产生,所以当你按下一个按钮,将出现一个随机的事实。但是我无法想象如何去做的事情是说我有多少事实。我想在按钮下方显示“有#事实可用”,如果我添加更多事实,则要更新数字。这是我的发电机。加起来JavaScript语句
<br />
<script type="text/javascript">
document.write("<br>")
function onClick() {
function generateRandomFact(first, last) {
return Math.floor(Math.random() * (last - first + 1)) + first;
}
randomfactno = generateRandomFact(1, 15)
if (randomfactno == 1) {
alert("Dragonflies can't walk, despite having legs.");
}
else if (randomfactno == 2) {
alert("The chewing sounds from Bugs Bunny were made by chewing real carrots.");
}
else if (randomfactno == 3) {
alert("Yelling for 8 and a half years creates enough energy to heat 1 coffee cup.");
}
else if (randomfactno == 4) {
alert("Sign language speakers can speak in their sleep using sign language.");
}
else if (randomfactno == 5) {
alert("4 bits = 1 nibble.");
}
else if (randomfactno == 6) {
alert("German chocolate cake was made by an American.");
}
else if (randomfactno == 7) {
alert("Silver is predicted to run out by 2020, due to industrial use.");
}
else if (randomfactno == 8) {
alert("The first Youtube video was of Jawed Karim talking about elephants.");
}
else if (randomfactno == 9) {
alert("The Golden Gate Bridge's color is International Orange.");
}
else if (randomfactno == 10) {
alert("August 26th is International Dog Day.");
}
else if (randomfactno == 11) {
alert("A cat named Meow weighed 39.6 pounds (18.0 kilograms), making him the heaviest cat in the world at the time, but not the heaviest on record.");
}
else if (randomfactno == 12) {
alert("Jean-Paul Sartre, awarded the 1964 Nobel Prize in Literature, declined the prize because he had consistently declined all official honours.");
}
else if (randomfactno == 13) {
alert("Presidents on other US Currency: William McKinley $500 bill, Grover Cleveland $1,000 bill, James Madison $5,000 bill, and Salmon P. Chase $10,000 bill.");
}
else if (randomfactno == 14) {
alert("The aurora at the south pole is called the aurora australis.");
}
else if (randomfactno == 15) {
alert("The jalapeno was the first pepper to travel into space.");
}
else {
alert("Javascript Error.");;
}
}
</script>
<div class="button">
<button onclick="onClick()">Generate Random Fact!</button>
</div>
</center>
此外,有没有人知道一种方法,使这更好。即使有15个事实,这个生成器也会重复相同的事实。
感谢您的帮助。