samedi 22 mai 2021

Math.random() not working as expected in for loop in React app

I'm trying to build a function for a lorem ipsum page that takes an array of words, number of paragraphs, min/max numbers of sentences per paragraph, and min/max numbers of words per sentence. It should build array of paragraphs, with each paragraph containing an array of sentences, with each sentence containing an array of words. There should be a random number of sentences in each paragraph, between the min/max sentence parameters, and a random number of words in each sentence, between the min/max words parameters. The array of words that is used in the parameter has been randomized prior to this function and has a length equal to maxWords * maxSentences * numParagraphs

Full code below, but the issue I'm having is that for some reason the random numbers that are being generated for each sentence/paragraph are outside of the min/max bounds. The code used to generate the random numbers works fine in a Javascript sandbox, but is throwing out huge numbers when inside this function. I've included console.log statements I was trying to use to test what could be going wrong, as well as a screenshot of the console output after running the code. The console output came from running the function with the following parameters: numParagraphs=3, minWords=3, maxWords=5, minSentences=2, maxSentences=6.

generateParagraphObj(numParagraphs, minSentences, maxSentences, minWords, maxWords, wordArr){
    console.log('generateParagraph ran')
    let output = [];
    let wordArrayInd = 0;
    let numSentences;
    let numWords;
    console.log(Math.floor(Math.random() * (maxSentences + 1 - minSentences)) + minSentences);
    //for every paragraph
    for(let a = 0; a < numParagraphs; a++){
        console.log('paragraph built')
        let paragraph = [];
        //determine number of sentences
        console.log(`Sentence min is ${minSentences}`);
        console.log(`Sentence max is ${maxSentences}`);
        console.log(Math.floor(Math.random() * (maxSentences + 1 - minSentences)) + minSentences);
        numSentences = Math.floor(Math.random() * (maxSentences + 1 - minSentences) + minSentences);
        //for every sentence
        for(let b = 0; b < numSentences; b++){
            //determine number of words
            console.log('sentence built');
            numWords = Math.floor(Math.random() * (maxWords + 1 - minWords) + minWords);
            let sentence = wordArr.slice(wordArrayInd, wordArrayInd + numWords + 1);
            //update index in array of words to move onto next unused word for next sentence
            wordArrayInd = wordArrayInd + numWords;
            //push sentence into paragraph
            // paragraph.push(sentence);
            paragraph.push('test sentence');
        }
        //push paragraph into output
        output.push(paragraph);
    }
    return output;
}

console output

I know that there are more efficient ways to build the arrays I want, but all I want to know is why the Math.random() lines here aren't working.

Thanks!




Aucun commentaire:

Enregistrer un commentaire