添加 JavaScript 语句

Adding up JavaScript statements

本文关键字:语句 JavaScript 添加      更新时间:2023-09-26

我创建了一个随机事实生成器,所以当你按下按钮时,会出现一个随机事实。但是我不知道该怎么做的是说我有多少事实。我想在按钮下方说"有#事实可用",如果我添加更多事实,数字就会更新。这是我对发电机的准备。

<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 个事实,该生成器也会大量重复相同的事实。

感谢您的帮助。

将事实放在一个数组中,并执行array.length来获取事实的数量,同时使您的代码更加干燥

var facts = [
    "Dragonflies can't walk, despite having legs.",
    "The chewing sounds from Bugs Bunny were made by chewing real carrots.",
    "Yelling for 8 and a half years creates enough energy to heat 1 coffee cup.",
    ...
]
var numberOfFacts = facts.length;
function onClick() {
    function generateRandomFact(first, last) {
        return Math.floor(Math.random() * (last - first + 1)) + first;
    }
    var randomfactno = generateRandomFact(0, 14);
    alert( facts[randomfactno] );
}

如果您不希望它们重复,可以随时弹出它们

    var randomfactno = generateRandomFact(0, 14);
    alert( facts[randomfactno] );
    facts = facts.splice(randomfactno, 1);

它真的可以做得小很多。我不喜欢使用 document.write,因为它覆盖了 DOM,但您已经在使用它,所以我认为出于说明目的会很好。

var facts = [ 
  "Dragonflies can't walk, despite having legs.",
  "The chewing sounds from Bugs Bunny were made by chewing real carrots.",
  "Yelling for 8 and a half years creates enough energy to heat 1 coffee cup.",
  "Sign language speakers can speak in their sleep using sign language.",
  "4 bits = 1 nibble.",
  "German chocolate cake was made by an American.",
  "Silver is predicted to run out by 2020, due to industrial use.",
  "The first Youtube video was of Jawed Karim talking about elephants.",
  "The Golden Gate Bridge's color is International Orange.",
  "August 26th is International Dog Day.",
  "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.",
  "Jean-Paul Sartre, awarded the 1964 Nobel Prize in Literature, declined the prize because he had consistently declined all official honours.",
  "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.",
  "The aurora at the south pole is called the aurora australis.",
  "The jalapeno was the first pepper to travel into space."
];
document.write("There are " + facts.length + " facts.");
function onClick() {
  alert(facts[Math.floor(Math.random() * (0, facts.length-1)) + 0]);
}