配列内の単語やフレーズからランダムな文を生成するためのスクリプトを作成しました。これは機能します。関数を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(" ");
}
私は新しい配列が実行addCommaAfterを言うとfullSentWithEndに渡したrandParagraphに考え、そして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