Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

179
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda