garrethwills garrethwills - 3 months ago 9
Javascript Question

loop a function a random number of times in javascript

I have created a script to generate random sentences from words and phrases in an array. This works.

I want to make a paragraph from these random sentences. What I have done though is repeating the same sentence, rather than running the function again and again to create new sentences.

I think my error is in this part of the code.

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


fullSentWithEnd is the final function in generating the sentences.

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


and addAfterComma is splitting the sentence to add a comma.

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(" ");


}

I thought in randParagraph the new array was saying run addCommaAfter and pass in fullSentWithEnd, and tell it to run a random number of times between 5 and 12. But now I am wondering if it is actually saying that, or if that is what is telling it to repeat the same result.

Would love some thoughts.



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)));




Answer

you are seeding the addCommaAfter func from randParagraph func with same sentence. instead change the seeding with random sentences like following, it will create paragraph with random sentences.

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

Full code:

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)));