我创建了一个脚本,可以从数组中的单词和短语生成随机句子。这可行。

我想用这些随机句子作一个段落。不过,我要做的是重复相同的句子,而不是一次又一次地运行该函数以创建新的句子。

我认为我的错误在于代码的这一部分。

const randParagraph = (len, end, words, wordLen) =>
[...Array(len)].map(() =>  addCommaAfter(fullSentWithEnd,sentLength(5,12)))
.join(' ') + (end || ' ');


fullSentWithEnd是生成句子的最终功能。

const fullSentWithEnd = randSentence(ipsumText, sentLength(5,12), '.')


并且addAfterComma将句子拆分以添加逗号。

const addCommaAfter = (sentence, index) => {
word_split = sentence.split(" ");
word_split[index] = word_split[index]+",";
word_split[0] = word_split[0][0].toUpperCase() + word_split[0].slice(1);
return word_split.join(" ");


}

我以为在randParagraph中,新数组是说运行addCommaAfter并传递fullSentWithEnd,并告诉它运行5到12之间的随机次数。但是现在我想知道它实际上是在说那个,还是在说什么它重复相同的结果。

会喜欢一些想法。



const ipsumText = ["adventure", "endless youth", "dust", "iconic landmark", "spontaneous", "carefree", "selvedge","on the road", "open road", "stay true", "free spirit", "urban", "live on the edge", "the true wanderer", "vintage motorcyle", "american lifestyle", "epic landscape", "low slung denim", "naturaL"];

const randInt = (lower, upper) =>
Math.floor(Math.random() * (upper-lower)) + lower

const randWord = (words) => words[randInt(0, words.length)]

const randSentence = (words, len, end) =>
[...Array(len)].map(() => randWord(words)).join(' ') + (end || ' ')

const randWordWithEnd = (end) => randWord(ipsumText) + end
const randWordWithFullStop = randWordWithEnd('. ')
const randWordWithComma = randWordWithEnd(', ')

const sentLength = (min,max) => {return Math.floor(Math.random() * (max - min + 1)) + min;};

const fullSentWithEnd = randSentence(ipsumText, sentLength(5,12), '.')
const fullSentNoEnd = randSentence(ipsumText, sentLength(5,12))
const fullSentComposed = fullSentNoEnd + randWordWithFullStop

const addCommaAfter = (sentence, index) => {
	word_split = sentence.split(" ");
	word_split[index] = word_split[index]+",";
	word_split[0] = word_split[0][0].toUpperCase() + word_split[0].slice(1);
	return word_split.join(" ");
}

console.log(fullSentWithEnd)
console.log(" ");
console.log(addCommaAfter(fullSentWithEnd,sentLength(3,8)));

const randParagraph = (len, end, words, wordLen) =>
[...Array(len)].map(() => addCommaAfter(fullSentWithEnd,sentLength(5,12)))
.join(' ') + (end || ' ');

console.log(randParagraph(sentLength(5,8), '', ipsumText, sentLength(5,12)));

最佳答案

您正在使用相同的句子从randParagraph func播种addCommaAfter函数。而是使用以下随机句子更改种子,它将创建带有随机句子的段落。

const randParagraph = (len, end, words, wordLen) =>
    [...Array(len)].map(() => addCommaAfter(randSentence(words, sentLength(5, 12), '.'), sentLength(5, 12)))
        .join(' ') + (end || ' ');


完整代码:



const ipsumText = ["adventure", "endless youth", "dust", "iconic landmark", "spontaneous", "carefree", "selvedge", "on the road", "open road", "stay true", "free spirit", "urban", "live on the edge", "the true wanderer", "vintage motorcyle", "american lifestyle", "epic landscape", "low slung denim", "naturaL"];

const randInt = (lower, upper) =>
    Math.floor(Math.random() * (upper - lower)) + lower

const randWord = (words) => words[randInt(0, words.length)]

const randSentence = (words, len, end) =>
    [...Array(len)].map(() => randWord(words)).join(' ') + (end || ' ')

const randWordWithEnd = (end) => randWord(ipsumText) + end
const randWordWithFullStop = randWordWithEnd('. ')
const randWordWithComma = randWordWithEnd(', ')

const sentLength = (min, max) => { return Math.floor(Math.random() * (max - min + 1)) + min; };

const fullSentWithEnd = randSentence(ipsumText, sentLength(5, 12), '.')
const fullSentNoEnd = randSentence(ipsumText, sentLength(5, 12))
const fullSentComposed = fullSentNoEnd + randWordWithFullStop

const addCommaAfter = (sentence, index) => {
    word_split = sentence.split(" ");
    if (index >= word_split.length) {
        index = word_split.length - 1;
    }
    word_split[index] = word_split[index] + ",";
    word_split[0] = word_split[0][0].toUpperCase() + word_split[0].slice(1);
    return word_split.join(" ");
}

console.log(fullSentWithEnd)
console.log(" ");
console.log(addCommaAfter(fullSentWithEnd, sentLength(3, 8)));

const randParagraph = (len, end, words, wordLen) =>
    [...Array(len)].map(() => addCommaAfter(randSentence(words, sentLength(5, 12), '.'), sentLength(5, 12)))
        .join(' ') + (end || ' ');

console.log(randParagraph(sentLength(5, 8), '', ipsumText, sentLength(5, 12)));

关于javascript - 在javascript中循环一个函数随机次数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39331182/

10-11 23:06
查看更多