外星人名字生成器

时间:2017-09-23 14:14:30

标签: javascript jquery html

我在我的javascript课程中有一个作业,它提出了一个问题,我觉得我已经接近能够回答,但还没到达那里。

令我困惑的一件事是,为什么开发人员工具会告诉我变量" message"一片空白。我无法弄明白。在这个问题出现之前的所有事情(都是类似的问题),我已经能够轻而易举地完成任务。我可能在想它。

问题: "创建一个应用程序,生成一组五个随机的"外星人名称"单击按钮时。这些外星人的名字必须至少有一个元音,名字中应该有两个重复的字符。示例名称包括:' llag',' Uffrd'和' Dxxi'。

这些名称应该在DOM上显示给用户,并在后续按钮点击时替换为新名称。"

我的代码(一直在摆弄它并且在粘贴之前没有检查过,所以请原谅我,如果它很难导航。让我知道如何你会用javascript来解决这个问题。



    //declares and assaigns the message (text where names will be printed) 
    var message = document.querySelector("#message");
    //declares the array and gives it 5 spots for names
    var nameArray = ["", "", "", "", ""];

    // declares the name generating function
    function rando() {
        //declares assigns name as empty for now
        var name = [];
        //randomizes length of the name
        var namelength = Math.round(Math.random() * 7) + 4;
        //declaring a vowel count
        var vowelCount = 0;
        //for loop to store each name into the array
        for (var i = 0; i < 5; i++) {
            //for loop to decide the letters for each
            for (var i = 0; i < namelength; i++) {
                //declares boolean variable that stores whether or not the current character is a vowel using the created is Vowel() function
                var wasVowel = isVowel(name[i]);
                //if the last character was a vowel
                if (wasVowel) {
                    //counts vowels
                    vowelCount++;
                    //while loop
                    while (isVowel(name[i])) {
                        //declares the variable and assigns it to a random number between 0 and 25
                        var randomCharacterIndex = Math.round(Math.random() * 25);
                        //updates the current character based on the random variable equal to or above the unicode 97 ("a")
                        name[i] = String.fromCharCode(97 + randomCharacterIndex);
                    }
                    //if the previous character was not a vowel
                } else {
                    //while loop
                    while (isVowel(name[i]) == false) {
                        //declares variable and assigns it to random number between 0 and 25
                        var randomCharacterIndex = Math.round(Math.random() * 25);
                        //updates the current character based on the random variable equal to or above the unicode 97 ("a")
                        name[i] = String.fromCharCode(97 + randomCharacterIndex);
                    }
                }
                //adds each letter to the name
                name += name[i];
            }
            //making the first letterz
            name[0].toUpperCase();
            //adds each name to the array
            nameArray[i] = name.join("");
            //name is reset to null for the next name loop
            name = [];
        }
        //prints the names onto the DOM
        message.innerHTML = nameArray.join(", ");
    }
    

    function isVowel(character) {
        if (character == "a" || character == "e" || character == "i" || character == "o" || character == "u") {
            return true;
        } else {
            //this is the 'default'
            return false;
    }
}
&#13;
        <button onclick="rando()">Random Names</button>
        <div id="message"></div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我使用以下方法

  1. 生成随机名称长度

  2. 生成随机双字符位置

  3. 生成与两个重复不相交的强制性元音位置 人物
  4. 构建单词,遵循设置规则并相应地生成随机字符

    var message = document.querySelector("#message");
    
    function rando() {
    
       message.innerHTML = "";
    
        for (var j=0;j<5;j++)
        {
          var name = "";
          var namelength = Math.round(Math.random() * 7) + 4;
          var pairPosition = Math.round(Math.random() * (namelength-1));
          var obligatoryVowelPos = Math.round(Math.random() * (namelength-1));
    
    
    
          while(obligatoryVowelPos == pairPosition || obligatoryVowelPos == (pairPosition+1))
          obligatoryVowelPos = Math.round(Math.random() * (namelength-1));
    
    
           for (var i =0;i<namelength;i++) {
    
    
            if (i==pairPosition)
            {
                var character = generateRandomLetter();
                name = name + character + character;
                i++;
            }
            else if (i==obligatoryVowelPos)
            {
               name = name + generateVowel();
            }
            else
            {
               name = name+generateRandomLetter();
            }
    
            name = name.charAt(0).toUpperCase() + name.slice(1);
    
        //prints the names onto the DOM
    
        }
        message.innerHTML += name + "<br>";
        }
    
    }
    
    function generateVowel()
    {
          var aeiou = Math.round(Math.random()*4)+1;
    
          switch (aeiou)
          {
              case 1:return 'a';break;
              case 2:return 'e';break;
              case 3:return 'i';break;
              case 4:return 'o';break;
              case 5:return 'u';break;
          }
    }
    
    function generateRandomLetter()
    {
        var randomCharacterIndex = Math.round(Math.random() * 25);
        return String.fromCharCode(97 + randomCharacterIndex);
    }
    
  5. 虽然不是必需的,但您也可以使用原始的isVowel函数来防止代码进入时出现双元音,或者如果在单词创建循环结束后没有添加元音,则可以使用元音添加元音。这完全取决于您,此代码已经解决了您的问题。

相关问题