0

I am making a word game that picks 8 random letters from the alphabet and the player must find words that use these letters. I am trying to find out how to stop it from showing duplicate letters.

Here is the code I am using to pick the letters

function makeid(length) {
    var result           = '';
    var characters       = 'aaabcdeeefghiiijklmnooopqrstuuuvwxyz';
    var charactersLength = characters.length;
    for ( var i = 0; i < length; i++ ) {
      result += characters.charAt(Math.floor(Math.random() * 
 charactersLength));
   }
   return result;
}

console.log(makeid(8));
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
pigeoned
  • 15
  • 4
  • 1
    i think simply getting non-dupe generator wont fix it since he has 3 indices on getting each vowel. returning 0,1,2 will yield `a` on all instances. I suggest OP to the letter generated if it is in the result before appending – NightEye May 05 '22 at 00:31
  • Use this inside your loop: `let letter = characters.charAt(Math.floor(Math.random() * charactersLength)); while(result.match(letter)) { letter = characters.charAt(Math.floor(Math.random() * charactersLength)); } result += letter;` – NightEye May 05 '22 at 00:39

0 Answers0