Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
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;
    }
  }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Ok I changed the way I thought about it lol I don't know why I was trying with these kinds of loops... possibly because it was super late yesterday Here is what worked for me:

const NUM_HINT_LETTERS = Math.ceil(blankLetters.length * 50/100); //ceil.50% of the words should stay visible as hints
  let hintPlacement = []; //array that matches the letters
  $.each(blankLetters, function(i) {
    let hint;
    if (i < NUM_HINT_LETTERS - 1) {
      hint = true; //the first NUM_HINT_LETTERS of elements are true and wont be changed to a blank space
    }
    else {
      hint = false;
    }
    hintPlacement.push(hint);
  });
  shuffle(hintPlacement); //give the hints random positions

  $.each(blankLetters, function(i) {
    if (!hintPlacement[i]) {
      blankLetters[i] = '?'
    }
  });
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda