vendredi 11 mars 2022

How can I remove a specific amount of items from an array but at random locations without loosing the array order?

I'm trying to replace a percentage of letters in an array containing single letter strings. I'm trying to do it with a while loop but it doesn't seem to work here's what I've tried:

  let validAnswer = false;
  let neededHintLetters = Math.round(blankLetters.length * 40/100);
  while(!validAnswer) {
    let numHintLetters = 0;
    $.each(blankLetters, function(i) {
      let index = Math.floor(Math.random() * blankLetters.length);

      if (index !== i) {
        blankLetters[i] = '?'
      }
      else { //don't replace the letter
        numHintLetters++;
      }
    });
    if (numHintLetters === neededHintLetters) {
      break;
    }
  }



Aucun commentaire:

Enregistrer un commentaire