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

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

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 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!