我正在构建我的第一个随机报价机器freecode camp项目。我需要我的javascript代码才能工作
var quotes = [["Imitation is suicide.", "-Ralph Waldo Emerson"] ["Flatter yourself critically.", "-Willis Goth Regier" ] ["Don’t look for society to give you permission to be yourself.", "-Steve Maraboli"] ["If things go wrong, don’t go with them.", "-Roger Babson"] ["Wanting to be someone else is a waste of who you are.", "-Kurt Cobain"] ["Do what you can, with what you have, where you are.", "-Theodore Roosevelt"] ["If you cannot be a poet, be the poem.", "-David Carradine"] ["Be there for others, but never leave yourself behind.", "-Dodinsky"]];
function newQuote() {
var randomNumber = math.floor(math.random() * (quotes.length));
document.getElementById('quoteDisplay').innerHTML = quotes[randomNumber];
}
答案 0 :(得分:0)
您忘记了数组中的逗号
var quotes = [["Imitation is suicide.", "-Ralph Waldo Emerson"], ["Flatter yourself critically.", "-Willis Goth Regier" ], ["Don’t look for society to give you permission to be yourself.", "-Steve Maraboli"], ["If things go wrong, don’t go with them.", "-Roger Babson"], ["Wanting to be someone else is a waste of who you are.", "-Kurt Cobain"], ["Do what you can, with what you have, where you are.", "-Theodore Roosevelt"], ["If you cannot be a poet, be the poem.", "-David Carradine"], ["Be there for others, but never leave yourself behind.", "-Dodinsky"]];
function newQuote() {
var randomNumber = Math.floor(Math.random() * (quotes.length));
document.getElementById('quoteDisplay').innerHTML = quotes[randomNumber];
}
newQuote();

<div id="quoteDisplay"></div>
&#13;
答案 1 :(得分:0)
您的代码存在一些问题:
数组中的元素应以逗号分隔。
所以不应该写[["quote1", "author1"] ["quote2", "author2"]]
,而应该是[["quote1", "author1"],["quote2", "author2"]]
。
Window对象下没有任何名为math
的内容,它是Math
。因此,您应该拨打math.floor
。
Math.floor
quotes[randomNumber]
为您提供引号字符串和作者的数组。因此,当您写入元素的innerHTML
时,它将转换为字符串。你可以通过显式调用quotes[randomNumber].join("")
来避免逗号,或者你可以声明一个不同的元素分隔符,例如那里的新行。
var quotes = [["Imitation is suicide.", "-Ralph Waldo Emerson"], ["Flatter yourself critically.", "-Willis Goth Regier" ], ["Don’t look for society to give you permission to be yourself.", "-Steve Maraboli"], ["If things go wrong, don’t go with them.", "-Roger Babson"], ["Wanting to be someone else is a waste of who you are.", "-Kurt Cobain"], ["Do what you can, with what you have, where you are.", "-Theodore Roosevelt"] ,["If you cannot be a poet, be the poem.", "-David Carradine"], ["Be there for others, but never leave yourself behind.", "-Dodinsky"]];
function newQuote() {
var randomNumber = Math.floor(Math.random() * (quotes.length));
document.getElementById('quoteDisplay').innerHTML = quotes[randomNumber].join("<br>");
}
newQuote();
&#13;
<div id="quoteDisplay">
</div>
&#13;
答案 2 :(得分:0)
首先您以错误的格式声明数组插入&#34; ,&#34;在数组中的元素之间。
第二没有数学类,而是使用数学类。