我想问一下随机字符串生成器的挑战。所以,我从没有淀粉出版社出版的儿童书中获得了这个测验。我是新手,对这个挑战非常困惑,有人可以帮助我吗?
====================
"制作一个随机字符串生成器。你需要从一开始 包含字母表中所有字母的字符串: var alphabet =" abcdefghijklmnopqrstuvwxyz&#34 ;; 要从此字符串中选择一个随机字母,您可以 更新我们用于随机侮辱发生器的代码 第3章:Math.floor(Math.random()* alphabet.length)。这个 将在字符串中创建一个随机索引。然后你可以使用 用方括号来获取该索引处的字符。 要创建随机字符串,请以空字符串开头 (var randomString ="")。然后,创建一个while循环 不断向这个字符串添加新的随机字母 因为字符串长度小于6(或您选择的任何长度)。 您可以使用+ =运算符向末尾添加新字母 的字符串。循环完成后,将其记录到控制台 看到你的创作!"
答案 0 :(得分: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);
&#13;