简体   繁体   English

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

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

I am trying to make a random quote generator that generates a quote by the click of a button.我正在尝试制作一个随机报价生成器,通过单击按钮生成报价。 I think I have done everything right, but once the button is clicked nothing happens.我想我做的一切都是对的,但是一旦点击了按钮,什么也没有发生。

Here is the code这是代码

HTML code 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 code 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];`
}

Mistakes in code代码错误

  • Function call should be done on click with onclick="newQuote()" and not with onclick="function newQuote()" Function 调用应该在单击onclick="newQuote()"而不是onclick="function newQuote()"
  • Typo error in taking the length of list to generate random number.取列表长度生成随机数时出现拼写错误。 It should be const randomNumber = Math.floor(Math.random() * list.length);它应该是const randomNumber = Math.floor(Math.random() * list.length); and not const randomNumber = Math.floor(Math.random() * list.lenght);而不是const randomNumber = Math.floor(Math.random() * list.lenght);
  • Id specified in HTML was quote-generator and in script was quotes-generator .在 HTML 中指定的 ID 是quote-generator ,在脚本中是quotes-generator They must be same.它们必须相同。
  • Also you can move the random number generation logic to inside of the function to generate new random number each time the user clicks the button.您还可以将随机数生成逻辑移至 function 内部,以便在用户每次单击该按钮时生成新的随机数。 Current random number generation happens only once and the value will not update when the user clicks the button当前的随机数生成只发生一次,当用户单击按钮时该值不会更新

 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>

The minimal representation of your solution will be您的解决方案的最小表示将是

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

It should be list.length and not list.lenght.它应该是 list.length 而不是 list.lenght。

  1. In your example no need to escape character in array.在您的示例中,无需转义数组中的字符。 If single quote ' inside in your array value you need escape character.如果数组值中的单引号'需要转义字符。
  2. End of array you have extra , .数组末尾有额外的,
  3. Wrong way to call function on button click.单击按钮时调用 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>

If you want to avoid repetitions, it is probably a better idea to shuffle the array once and then present it to the user in a cycling fashion:如果您想避免重复,最好将数组打乱一次,然后以循环方式将其呈现给用户:

 /* 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.

相关问题 谁能告诉我这个套接字事件有什么问题? - Can anyone please tell me what's wrong this socket event? 谁能告诉我这个switch语句(javascript)怎么了? - Can anyone tell me what's wrong with this switch statement (javascript)? 谁能告诉我这个脚本出了什么问题? - Can anyone tell me what's wrong with this script? 我的Jquery按钮-禁用/启用功能不起作用。 谁能告诉我怎么了? - My Jquery button- disable/enable function is not working. Can anyone tell me what's wrong? 谁能告诉我我的代码有什么问题 - Can anyone tell me what is wrong in my code 谁能告诉我我的代码有什么问题? - Can anyone tell me what is wrong with my code? 我如何 map 反应 js 中的一个数组我试过了但是有一些问题谁能帮我 - how do i map an array in react js i have tried but there is some problem can anyone help me 试图抓住输入值并显示,谁能告诉我我在这里做错了什么? - Trying to grap the input value and display, Can anyone tell me what i'm doing wrong here? 谁能告诉我是什么让这段代码无法正确呈现? 我感觉如此接近 - Can anyone tell me what's keeping this code from rendering correctly? I feel so close 我正在学习 JavaScript 谁能告诉我我做错了什么? 总和应该给我和8 - Im learning JavaScript can anyone tell me what Im doing wrong? The sum is supposed to give me and 8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM