This question sets itself apart from Removing random letters from a string as it focuses on selecting a random letter from a string in JavaScript without removing any characters.
The goal is to implement a code that picks random letters from a string in JavaScript using Math.floor(Math.random()* string.length) along with a while loop. The program should continually add new random letters to the string until reaching a specified length.
The current code snippet looks like this:
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);
Issues: The output displays the same letter repeated six times: e.g., pppppp
The random letter is only generated once and then duplicated until the required length is reached. The desired outcome would be unique, random output for each letter added: e.g., pwezjm
Furthermore, running a separate while loop over the same string results in identical output as the initial loop: e.g., pppppp
One might expect the second loop to generate different random letters than the first, but it fails to do so. What could be causing this behavior?