I have four arrays - flowers, trees, critters and songs.
I have four corresponding functions - flower(), tree(), critter() and song().
Within each individual function above, I have called my math.random function to retrieve a random word from the specific array. For example, in my flower() function I have:
answer = flowers[Math.floor(Math.random() * flowers.length)];
I have found that in order for the random word to be generated, I also need to run my math.random function globally. Here is my problem. I don't know of a way to create the math.random function without assigning an array. So globally I have this:
function randomWord() {
answer = songs[Math.floor(Math.random() * songs.length)];
}
So sometimes when I am in the flower category, a word from my flower array will show up - but sometimes a song will show up instead because I had to post it globally for it to work at all.
I am at a loss for how to fix it and while there are many questions regarding the math.floor function for arrays - I've not found any information on how to handle multiple arrays like this.
I'm wondering if there is a general word I could use instead of a specific array word for the part math.floor function that is global? Or perhaps an if-else statement? I'm stuck and would appreciate any guidance. Thank you.
Aucun commentaire:
Enregistrer un commentaire