添加JavaScript语句

时间:2016-01-22 17:27:32

标签: javascript html

我创建了一个随机事实生成器,因此当您按下按钮时,会出现随机事实。但我无法弄清楚如何做的事情就是说我有多少事实。我想说"有#个事实可用"如果我添加更多事实,则在按钮下方以及要更新的号码。这就是我对发电机的所作所为。

<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个事实,这个发生器也会重复相同的事实。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

将事实放在数组中,然后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);

答案 1 :(得分:0)

它真的可以变得更小。我不喜欢使用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]);
}