2016-01-22 54 views
0

我创建一个随机事实产生,所以当你按下一个按钮,将出现一个随机的事实。但是我无法想象如何去做的事情是说我有多少事实。我想在按钮下方显示“有#事实可用”,如果我添加更多事实,则要更新数字。这是我的发电机。加起来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个事实,这个生成器也会重复相同的事实。

感谢您的帮助。

回答

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); 
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]); 
}