Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
¿Cómo puedo eliminar una cantidad específica de elementos de una matriz pero en ubicaciones aleatorias sin perder el orden de la matriz?

Estoy tratando de reemplazar un porcentaje de letras en una matriz que contiene cadenas de una sola letra. Estoy tratando de hacerlo con un bucle while pero parece que no funciona, esto es lo que he intentado:

 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 answers
Answer question

0

Ok, cambié la forma en que lo pensaba jajaja, no sé por qué estaba intentando con este tipo de bucles... posiblemente porque ayer era muy tarde. Esto es lo que funcionó para mí:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!