gpt4 book ai didi

javascript - 如何打印这条随机线?

转载 作者:行者123 更新时间:2023-12-02 21:28:20 26 4
gpt4 key购买 nike

这是我的代码

var e=document.getElementById('button');
e.addEventListener('click',function yo(e)
{e.preventDefault();
var a=[
{'quote':"Result: something good is going to happen with you",},
{'quote':"Result: your future is very bright i think you are going to meet someone special in your life",},
{'quote':"Result: just be careful things are not looking good",},
{'quote':"Result: just be patient in your life if you want everything fine",},
{'quote':"Result: ohh its difficult please retry ",},
{'quote':"Result: daymn a good news is coming ",},
{'quote':"Result: be ready for the upcoming storm my friend",},
{'quote':"Result: i dont want to tell you ryt now",},

];
var count=0;
var b=document.getElementById('user');
var c=document.createElement('p');
var d=document.getElementById('ha');
var f="";
i=Math.floor(Math.random()*8);//Here alone Math.random*8 gives the number between 0 to 7 and always in float
//so to make it as whole number we put it inside Math floor
c=b.value;

var l=a[i].quote;
f="<p>Result: "+ c + +l+ "</p>"
d.insertAdjacentHTML('beforeend',f);
})

我的问题是,当我输入用户名时,它确实出现在一个段落中,但它没有显示引号,而是显示了 NaN

最佳答案

正是这里的这一行导致了问题:

f="<p>Result: "+ c + +l+  "</p>"

如果我们尝试按照 JavaScript 的理解来分解它,我们会得到

  • f = (作业)
  • "<p>Result: " (字符串)
  • + (串联)
  • c (c中的值)
  • + (串联)
  • +l (对 l 中的值进行一元运算)
  • + (串联)
  • "</p>" (字符串)

所以你正在连接一堆字符串,但是额外的+正在获取一个字符串并尝试将其转换为数字。正如您可以想象的那样,报价不是数字,因此 NaN

关于javascript - 如何打印这条随机线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60679202/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com