我有两个数组数组,我必须生成多个随机引号。用户必须在1到5个报价之间进行选择,以及要选择哪个发电机(1或2)。
我开始创建两个生成器,它们工作正常,但不能一起工作。 有帮助吗?
我是Java语言的新手。
// sentences for generatorOne
let userChoice;
let generatorChoice;
let first = [
['Peace,', 'Love', 'Money', 'iPhones', 'Beauty', 'Happines'],
[' will be', ' will not be', ' maybe wil be', ' will never be', ' must be ', ' must not be '],
[' your end', ' your start', ' your future', ' your tasks', ' your past '],
]
// sentences for generatorTwo
let second = [
['Tulips', 'Roses', 'Daisys', 'Azaleas', 'Begonias', 'Gardenias'],
[' are', ' are not', ' maybe are', ' maybe are not', ' have to be ', ' have not to be '],
['good ', ' awesome ', ' great ', ' red ', ' black '],
];
//generators
function generatorOne() {
var gOne = [];
for (let i = 0; i < first.length; i++) {
gOne.push(first[i][Math.floor(Math.random() *
first[i].length)]);
}
return gOne.join(" ");
}
function generatorTwo() {
var gTwo = [];
for (let i = 0; i < second.length; i++) {
gTwo.push(second[i][Math.floor(Math.random() *
second[i].length)]);
}
return gTwo.join(" ");
}
function generators() {
var mix = document.getElementById("userInput").value;
for (var i = 0; i < mix; i++) {
if (document.getElementById("quoteChoice").value === '1') {
generatorOne();
} else if (document.getElementById("quoteChoice").value === '2' {
generatorTwo();
}
}
}
function printQuote() {
var pTag = document.getElementById("demo");
pTag.innerText = generators();
}
printQuote();
答案 0 :(得分:0)
function generators()不返回任何内容。试试
function generators() {
var mix = document.getElementById("userInput").value;
for (var i = 0; i < mix; i++) {
if (document.getElementById("quoteChoice").value === '1') {
return generatorOne();
} else if (document.getElementById("quoteChoice").value === '2') {
return generatorTwo();
}
}
}