我正在研究这个带循环的报价生成器。生成器必须从数组 1 + 数组 2 + 数组 3 返回一个随机引用(例如“爱”“将是”“你的任务”),但是当它获得“和平”“将是”“你的开始”时,它必须发出警报: “你发现我了”。 我认为问题是 select.quote 函数不起作用
let array1 = [ " peace " , "love" , " money"];
let array2 = [ " will be " , "will never be ", "maybe will be"];
let array3 = [" your end", " your start", "your tasks"];
function finalQuote(...arrs) {
let quote = '';
for (let i = 0; i <arrs.length; i++) {
quote += arrs[i][Math.floor(Math.random() * 3)];{
return quote;}
select.quote = function(){
if (arrays){
if (array1 === 'peace', array2 === 'will be', array3 === 'your end'){
alert("you found me!");
}}else{
let FinalQuote =finalQuote(array1, array2, array3);}}
qt_btn.addEventListener("click", function(event) {
finalQuote();});
最佳答案
可以做很多事情来整理你的代码,但是下面的应该可以工作,有很多方法可以解决这个问题。
let array1 = [ "peace " , "love" , "money"];
let array2 = [ "will be " , "will never be ", "maybe will be"];
let array3 = ["your end", "your start", "your tasks"];
function checkStringForMatch (str, word) {
return str.indexOf(word) !== -1
}
function finalQuote(...arrs) {
let quote = '';
for (let i = 0; i < arrs.length; i++) {
quote += arrs[i][Math.floor(Math.random() * 3)] + ' ';
}
if (checkStringForMatch(quote, 'peace') && checkStringForMatch(quote, 'will be') && checkStringForMatch(quote, 'your end')){
alert("you found me!");
} else {
alert(quote)
}
}
qt_btn.addEventListener("click", function(event) {
finalQuote(array1, array2, array3);
});
关于javascript - 带循环 javascript 的报价生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54144487/