简体   繁体   English

在JavaScript中从字符串中选择一个随机字母

[英]Pick a random letter from string in JavaScript

This question is different from Take random letters out from a string because I am not trying to remove anything from the string. 这个问题不同于从字符串中取出随机字母,因为我不是要从字符串中删除任何内容。

I'm trying to pick a random letter from a string in JavaScript using Math.floor(Math.random()* string.length) and a while loop. 我正在尝试使用Math.floor(Math.random()* string.length)和while循环从JavaScript中的字符串中选择一个随机字母。 It needs to continually add new random letters to this string until a specified length. 它需要不断向该字符串添加新的随机字母,直到指定的长度。

My code is: 我的代码是:

var emptyString = "";
var alphabet = "abcdefghijklmnopqrstuvwxyz";
var getRandomLetter = alphabet[Math.floor(Math.random() * alphabet.length)];
var randomLetter = getRandomLetter;

while (emptyString.length < 6) {
emptyString += randomLetter;
emptyString ++;
} 
console.log(emptyString);

Problems: The output is the same letter 6 times: ex. 问题:输出是相同的字母6次:ex。 pppppp PPPPPP

The random letter is generated from the string only once and then repeated until the specified length. 随机字母仅从字符串生成一次,然后重复直到指定的长度。 I need it to generate random output for each letter: ex. 我需要它为每个字母生成随机输出:ex。 pwezjm pwezjm

I also noticed that if I do a second different while loop over the string it will generate the same output as the first loop: ex. 我还注意到,如果我在字符串上执行第二个不同的while循环,它将生成与第一个循环相同的输出:ex。 pppppp PPPPPP

I thought it would at least generate a different random letter then the first loop but it does not. 我认为它至少会产生一个不同的随机字母,然后是第一个循环,但事实并非如此。 Why is that? 这是为什么?

Because you should obtain the letter every time, but you do it just once. 因为你每次都应该获得这封信,但你只需要一次。

var emptyString = "";
var alphabet = "abcdefghijklmnopqrstuvwxyz";

while (emptyString.length < 6) {
  emptyString += alphabet[Math.floor(Math.random() * alphabet.length)];
} 
console.log(emptyString);

Also, not sure what you wanted to achieve with emptyString++ , removing that, since ++ is the "increment by one" operator and you can't incremenent a string. 另外,不确定你想用emptyString++实现什么,删除它,因为++是“递增一个”运算符,你不能增加一个字符串。 I think the purpose was to have it as a counter for that while loop, but it's needless, since the counter is the string length already. 我认为目的是让它作为while循环的计数器,但它是不必要的,因为计数器已经是字符串长度。

Get the random character in a loop loop获取随机字符

In your example, you are obtaining random character from array only once hence there is no way you are going to get another random character in while loop 在您的示例中,您只从数组中获取一次随机字符,因此您无法在while loop获得另一个随机字符

Also note that emptyString++ will cause result as NaN as you are trying to post-increment the string 另请注意,当您尝试对string进行后递增时, emptyString++将导致结果为NaN

 var emptyString = ""; var alphabet = "abcdefghijklmnopqrstuvwxyz"; while (emptyString.length < 6) { emptyString += alphabet[Math.floor(Math.random() * alphabet.length)]; } console.log(emptyString); 

Another tip, alphabet.length could be cached instead of asking for it every time in while 另一个技巧, alphabet.length可以被缓存,而不是每次都要求它while

While those above are nice. 虽然上面的那些很好。 I like shorter code. 我喜欢更短的代码。

const randomLetter = ('abcdefghijklmnopqrstuvwxyz').split('')[(Math.floor(Math.random() * 26 ))];

You need to change getRandomLetter into a function, and reassign randomLetter inside the loop like this: 您需要将getRandomLetter更改为函数,并在循环内重新分配randomLetter ,如下所示:

 var emptyString = ""; var alphabet = "abcdefghijklmnopqrstuvwxyz"; function getRandomLetter() { return alphabet[Math.floor(Math.random() * alphabet.length)]; } var randomLetter; while (emptyString.length < 6) { randomLetter = getRandomLetter(); emptyString += randomLetter; } console.log(emptyString); 

You also can't increment emptyString , since it is a string. 你也不能增加emptyString ,因为它是一个字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM