【问题标题】:Poem (random words) generator: How can I get each paragraph to display a different set of random words? [closed]诗歌(随机词)生成器:如何让每个段落显示一组不同的随机词? [关闭]
【发布时间】:2020-05-31 19:35:54
【问题描述】:

我正在做一个应该生成诗歌的小练习项目。我创建了几个包含不同类型单词的数组,然后我随机抓取它们并添加到段落中。它工作正常,但不是我想要的。

由于每个“短语”变量都是从同一组随机选择的单词中提取的,因此这些诗歌都是重复的。无论选择什么随机名词都会出现在每个名词槽中,每个随机动词出现在每个动词槽中等等。

我想要发生的是:每个插槽都会从数组中生成一个不同的随机词。如果随机选择了两个相同的单词,那没关系。

现在,每种类型的单词随机选择一次,并沿着所有句子重复。

我可以做些什么,因为我不会随时随机选择?

这是代码。

  

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

    <div id="poemBox">
    <div id="edgar">
      <img src="https://cdn.britannica.com/52/76652-050-F4A6B093/Edgar-Allan-Poe.jpg" width="150px" alt="avatar">
      <p>Press the button below for a poem from Poe.</p>
      <button onclick="makeAPoem()">Go</button>
    </div>
    <div id="poetry">
      <p class="poem"></p>
      <p class="poem"></p>
      <p class="poem"></p>
      <p class="poem"></p>
      <p class="poem"></p>
    </div>

在这里的Screenshot example 中,每出现一个随机形容词都使用“sweet”。每个随机动词都是“集中的”等等。

【问题讨论】:

    标签: javascript arrays string random


    【解决方案1】:

    现在您只需生成一次任何单词类型。 只需在任何阶段之前生成随机数

    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"];
    
      function randNoun(){return nouns[Math.floor(Math.random() * nouns.length)]}
      function randVerb(){return verbs[Math.floor(Math.random() * verbs.length)]}
      function randArticle(){return articles[Math.floor(Math.random() * articles.length)]}
      function randAdjective(){return adjectives[Math.floor(Math.random() * adjectives.length)]}
      function randPronoun(){return pronouns[Math.floor(Math.random() * pronouns.length)]}
    
      var phrase1 = randPronoun() + " " + randVerb() + " " + randAdjective();
      var phrase2 = randArticle() + " " + randNoun() + " " + randVerb() + " " + randAdjective();
      var phrase3 = randPronoun() + " " + randVerb() + ". " + randArticle() + " " + randNoun() + " " +  randVerb() + " " + randAdjective();
      var phrase4 = randArticle() + " " + randNoun() + " " + randVerb() + " " + randAdjective();
      var phrase5 = randPronoun() + " " + 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;
    }
    <div id="poemBox">
    <div id="edgar">
      <img src="https://cdn.britannica.com/52/76652-050-F4A6B093/Edgar-Allan-Poe.jpg" width="150px" alt="avatar">
      <p>Press the button below for a poem from Poe.</p>
      <button onclick="makeAPoem()">Go</button>
    </div>
    <div id="poetry">
      <p class="poem"></p>
      <p class="poem"></p>
      <p class="poem"></p>
      <p class="poem"></p>
      <p class="poem"></p>
    </div>

    【讨论】:

      猜你喜欢
      • 2013-10-17
      • 1970-01-01
      • 2017-05-10
      • 1970-01-01
      • 2013-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多