最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Pick a random letter from string in JavaScript - Stack Overflow

programmeradmin3浏览0评论

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. 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. 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. 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. pppppp

I thought it would at least generate a different random letter then the first loop but it does not. Why is that?

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. 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. 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. 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. pppppp

I thought it would at least generate a different random letter then the first loop but it does not. Why is that?

Share Improve this question edited Jul 27, 2016 at 18:33 Maytham Fahmi 33.4k16 gold badges127 silver badges153 bronze badges asked Jul 27, 2016 at 18:02 TYPOITYPOI 3892 gold badges6 silver badges19 bronze badges 2
  • This might help you: stackoverflow.com/a/1497512/1516112 – nikoskip Commented Jul 27, 2016 at 18:03
  • 1 You are only generating one random letter and not changing it. Why should it change? – Spencer Wieczorek Commented Jul 27, 2016 at 18:05
Add a comment  | 

4 Answers 4

Reset to default 11

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. 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.

Get the random character in a 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

Also note that emptyString++ will cause result as NaN as you are trying to post-increment the string

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

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:

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.

发布评论

评论列表(0)

  1. 暂无评论