dimanche 24 février 2019

On each submit to show a new random array class of random array subclass

I'm creating a lorem ipsum app and I want to click on a button to generate random quotes from a random character. And there will be no mixing of quotes between characters. However, every time I click submit it only shows random quotes from the first character I list in my array. How can I fix this?

const aQuotes = require("./public/quotes/a");
const bQuotes = require("./public/quotes/b");
const cQuotes = require("./public/quotes/c");

const loremIpsum = new GenerateNewText();
function GenerateNewText(){}

GenerateNewText.prototype.getRandomSentence = function() {
  const charQuotes =
  [
    aQuotes,
    bQuotes,
    cQuotes,
  ]
  for(var i = 0; i < charQuotes.length; i++){
    let randomSentence = charQuotes[i][Math.floor(Math.random() * charQuotes[i][Math.floor(Math.random())].length)]
    return randomSentence;
  }
}

When I run the example above, it'll show a random list of quotes stored in aQuotes with the word "undefined" sprinkled throughout. If I were to move bQuotes to the top of the array, then it will show random bQuotes only, also with the word "undefined". Why is it only showing the results of the first element in the array and why is "undefined" showing (i.e. aQuotes)?

const aQuotes = [
    "Lalalalalalalala.",
    "Blah blah blah blah.",
    "Blank Blank Blank Blank Blank." 
]

module.exports = aQuotes;

I tried doing charQuotes[i][Math.floor(Math.random())].length * charQuotes[i][Math.floor(Math.random())].length thinking that it will first randomize the charQuotes array and then randomize the individual a/b/cQuotes array but it ended up returning a block of the number 169. Other "fixes" I've attempted resulted in paragraphs of all undefined text, paragraphs of all NaN, or showing all the quotes for all the characters with the word "undefined" interjected here and there.

How can I randomize the charQuotes array AND the content in my a/b/cQuotes array with every click? And get rid of the weird "undefined" texts that come up?

I am using Node and Express.




Aucun commentaire:

Enregistrer un commentaire