2017-04-07 87 views
-3

我想问一下关于随机字符串生成器的挑战。所以,我从没有任何淀粉新闻出版商的孩子的JavaScript中获得了这个测验。我是新手,对这个挑战感到困惑,有人能帮助我吗?来自JavaScript的随机字符串生成器,适合孩子挑战

====================

随机字符串发生器

“做一个随机字符串生成器。你需要先从包含所有在字母表的字母 字符串: VAR字母=“ABCDEFGHIJKLMNOPQRSTUVWXYZ”; 要挑选此字符串从一个随机的信,你可以 更新我们 第3章用于随机侮辱生成的代码:Math.floor( Math.random()* alphabet.length)。这个 将创建一个随机索引到字符串中,然后可以使用 方括号表示该索引处的字符。 要创建随机字符串,请以空字符串 (var randomString =“”)开头。然后,创建一个while循环,将 不断向此字符串中添加新的随机字母,只要字符串长度小于6(或您选择的任何长度)为 。 您可以使用+ =运算符向字符串的末尾 添加一个新字母。 !循环完成后,登录到控制台 看你的创作”

回答

0

好吧,这些指令都有点内而外,这里以正确的顺序:

// You’ll need to start with a string containing all the letters in the alphabet: 
 

 
var alphabet = "abcdefghijklmnopqrstuvwxyz"; 
 

 
// To create the random string, start with an empty string 
 

 
var randomString = ""; 
 

 
// Then, create a while loop that will continually add new random letters to this string, as long as the string length is less than 6 (or any length you choose). 
 

 
while (randomString.length < 6) { 
 

 
    // To pick a random letter from this string, you can update the code we used for the random insult generator in Chapter 3 
 

 
    var randomIndex = Math.floor(Math.random() * alphabet.length); 
 
    
 
    // You can then use square brackets to get the character at that index. 
 

 
    var randomChar = alphabet[randomIndex]; 
 
    
 
    // You could use the += operator to add a new letter to the end of the string. 
 

 
    randomString += randomChar; 
 
    
 
} 
 
    
 
// After the loop has finished, log it to the console to see your creation! 
 

 
console.log(randomString);

+0

感谢georg,你真的帮我:) – szopen