Я создал скрипт для генерации случайных предложений из слов и фраз в массиве. Это работает.петля функция случайное число раз в javascript
Я хочу сделать абзац из этих случайных предложений. То, что я сделал, повторяет одно и то же предложение, вместо того, чтобы снова и снова запускать функцию для создания новых предложений.
Я думаю, моя ошибка находится в этой части кода.
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) => {
\t word_split = sentence.split(" ");
\t word_split[index] = word_split[index]+",";
\t word_split[0] = word_split[0][0].toUpperCase() + word_split[0].slice(1);
\t 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)));
вы можете дать код, который может быть запущен? –
просто добавил фрагмент выше @SufianSaory – garrethwills