繁体   English   中英

我尝试了多种方法,谁能告诉我这个数组有什么问题?

[英]I have tried multiple ways, Can anyone tell me what's wrong with this array?

我正在尝试制作一个随机报价生成器,通过单击按钮生成报价。 我想我做的一切都是对的,但是一旦点击了按钮,什么也没有发生。

这是代码

HTML代码

<div class="quote-box">
   <p id = "quote-generator"> this is where the quote will go </p>
   <button class="btn" onclick="function newQuote()"> Next </button>
</div>

JS代码

var list =  [
'/"Your mind will always believe what you tell it. Feed it faith. Feed it the truth. Feed it with love. /"',
'/"A problem is a chance for you to do your best./"',
'/"Learn how to be happy with what you have while you pursue all that you want./"',];`

const randomNumber = Math.floor(Math.random()*list.lenght);

function newQuote() {

document.getElementById("quotes-generator").innerHTML = list [randomNumber];`
}

代码错误

  • Function 调用应该在单击onclick="newQuote()"而不是onclick="function newQuote()"
  • 取列表长度生成随机数时出现拼写错误。 它应该是const randomNumber = Math.floor(Math.random() * list.length); 而不是const randomNumber = Math.floor(Math.random() * list.lenght);
  • 在 HTML 中指定的 ID 是quote-generator ,在脚本中是quotes-generator 它们必须相同。
  • 您还可以将随机数生成逻辑移至 function 内部,以便在用户每次单击该按钮时生成新的随机数。 当前的随机数生成只发生一次,当用户单击按钮时该值不会更新

 var list = [ '/"Your mind will always believe what you tell it. Feed it faith. Feed it the truth. Feed it with love. /"', '/"A problem is a chance for you to do your best./"', '/"Learn how to be happy with what you have while you pursue all that you want./"', ]; function newQuote() { const randomNumber = Math.floor(Math.random() * list.length); document.getElementById("quote-generator").innerHTML = list[randomNumber]; }
 <div class="quote-box"> <p id="quote-generator"> this is where the quote will go </p> <button class="btn" onclick="newQuote()"> Next </button> </div>

您的解决方案的最小表示将是

 const list = [ '"Your mind will always believe what you tell it. Feed it faith. Feed it the truth. Feed it with love. "', '"A problem is a chance for you to do your best."', '"Learn how to be happy with what you have while you pursue all that you want."', ]; newQuote = () => document.getElementById("quote-generator").innerHTML = list[Math.floor(Math.random() * list.length)];
 <div class="quote-box"> <p id="quote-generator"> this is where the quote will go </p> <button class="btn" onclick="newQuote()"> Next</button> </div>

const randomNumber = Math.floor(Math.random()*list.lenght);

它应该是 list.length 而不是 list.lenght。

  1. 在您的示例中,无需转义数组中的字符。 如果数组值中的单引号'需要转义字符。
  2. 数组末尾有额外的,
  3. 单击按钮时调用 function 的方式错误。

 var list = ['"Your mind will always believe what you tell it. Feed it faith. Feed it the truth. Feed it with love. "', '"A problem is a chance for you to do your best."', '"Learn how to be happy with what you have while you pursue all that you want."'];; function newQuote() { let rnd = Math.floor(Math.random() * list.length); let rqt = list[rnd]; document.getElementById("quote-generator").innerHTML = rqt; }
 <div class="quote-box"> <p id="quote-generator"> this is where the quote will go </p> <button class="btn" onclick="newQuote()"> Next </button> </div>

如果您想避免重复,最好将数组打乱一次,然后以循环方式将其呈现给用户:

 /* Randomize array in-place using Durstenfeld shuffle algorithm */ function shuffleArray(array) { for (let i = array.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [array[i],array[j]] = [array[j],array[i]]; } } const list = [ '"Your mind will always believe what you tell it. Feed it faith. Feed it the truth. Feed it with love. "', '"A problem is a chance for you to do your best."', '"Learn how to be happy with what you have while you pursue all that you want."', ]; shuffleArray(list); list.n=-1; document.querySelector(".btn").onclick=newQuote; function newQuote(){ document.getElementById("quote-generator").innerHTML = list[++list.n%list.length]; } newQuote();
 <div class="quote-box"> <p id="quote-generator"> this is where the quote will go </p> <button class="btn"> Next </button> </div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM