I'm working on a little practice project with Javascript that should generate poems. I created a couple of arrays full of different types of words, which I'm then grabbing randomly and adding into paragraphs. It's working, but not how I would like it to.
Since each "phrase" variable is pulling from the same set of randomly selected words, the poems are all repetitive. Whatever random noun is selected will appear in each noun slot, each random verb appears in every verb slot etc.
What I want to happen is that each slot will generate a different random word from the array. If a two of the same words are coincidentally selected at random, that's ok.
I don't want to create additional arrays of words to choose from unless that's literally the only way to do it. It seems like there's a more concise way to set up the function. Maybe using some sort of loop? Or adding another function that makes the random selection run again? I'm a novice, so I'm not sure what the best practice is.
Here's the code.
HTML
<div id="poetry">
<p class="poem"></p>
<p class="poem"></p>
<p class="poem"></p>
<p class="poem"></p>
<p class="poem"></p>
</div>
JS
// Note for when I pick this up:
// Create 2 more trios of words vars with rand vars. create phrase 1,2,3
function makeAPoem() {
var nouns = ["cat", "crow", "snow", "home", "boy", "raven", "tree", "moon", "night", "day", "winter", "heart", "angel", "madam", "darkness", "chamber", "lady", "bird", "person", "eye", "darkness", "air"];
var verbs = ["ran", "felt", "fell", "focused", "looked", "stared", "sat", "sighed", "blew", "whimpered", "embraced", "hugged"];
var articles = ["a", "the"];
var adjectives = ["shiny", "sad", "cold", "cheerfully", "sweet", "evil"];
var pronouns = ["I", "we", "you", "they", "she", "it", "he"];
var randNoun = Math.floor(Math.random() * nouns.length);
var randVerb = Math.floor(Math.random() * verbs.length);
var randArticle = Math.floor(Math.random() * articles.length);
var randAdjective = Math.floor(Math.random() * adjectives.length);
var randPronoun = Math.floor(Math.random() * pronouns.length);
var phrase1 = pronouns[randPronoun] + " " + verbs[randVerb] + " " + adjectives[randAdjective];
var phrase2 = articles[randArticle] + " " + nouns[randNoun] + " " + verbs[randVerb] + " " + adjectives[randAdjective];
var phrase3 = pronouns[randPronoun] + " " + verbs[randVerb] + ". " + articles[randArticle] + " " + nouns[randNoun] + " " + verbs[randVerb] + " " + adjectives[randAdjective];
var phrase4 = articles[randArticle] + " " + nouns[randNoun] + " " + verbs[randVerb] + " " + adjectives[randAdjective];
var phrase5 = pronouns[randPronoun] + " " + verbs[randVerb];
document.getElementsByClassName('poem')[0].innerHTML = phrase1;
document.getElementsByClassName('poem')[1].innerHTML = phrase2;
document.getElementsByClassName('poem')[2].innerHTML = phrase3;
document.getElementsByClassName('poem')[3].innerHTML = phrase4;
document.getElementsByClassName('poem')[4].innerHTML = phrase5;
}
Screenshot example: Every occurrence of a random adjective is using "sweet". Every random verb is "focused" and so on.
Aucun commentaire:
Enregistrer un commentaire