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

132
Views
Splicing random names from array until array is empty

I am trying to pick two random names and adding them together. Each time the name is picked, it should be removed from array. Function should run until array is empty. Output should be all names that were paired together in a string or array.

var names = ['Ben', 'Mike', 'Sally', 'Joe', 'Bob', 'Sam', 'Carl', 'Frank']
//var to pick two random names from an array
function pickTwo(arr) {
  let random = Math.floor(Math.random() * arr.length);
  let random2 = Math.floor(Math.random() * arr.length);
  while (random === random2) {
    random2 = Math.floor(Math.random() * arr.length);
  }
  //remove random and random2 from array
  //run untill there are no more names in the array
  while (arr.length > 0) {
    arr.splice(random, 1);
    arr.splice(random2, 1);
    return [arr[random], arr[random2]];
  }
}
console.log(pickTwo(names));
console.log(names);

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

First make sure you delete the 2 names you randomly selected from the array. The function you have does not do this because the arr variable that the names get deleted from is local to that function. Consider either using the global variable groovy that is initiated before the function, or returning the arr variable back from the function and then sending that back into it again. After that you simply have to make a while loop that runs the pickTwo function until there is nothing left in groovy.

var array = [];

while(groovy.length>1){
array.push(pickTwo(groovy));
}
about 4 years ago · Juan Pablo Isaza Report

0

You need to get new random numbers each time through the while (arr.length > 0) loop. Otherwise, your random indexes may be outside the array after you splice, and you get undefined.

You need to push each pair of numbers selected during the loop onto another array, and return that array as the final result.

Also, when you do the first splice, all the indexes after random shift down. If random2 is higher than random, you'll splice the wrong element. You should splice out the higher one first.

After you splice, arr[random] and arr[random2] don't refer to the elements that were selected, since you removed them from the array. splice() returns the removed elements, you should use that result.

A simpler way to do this whole thing would be to shuffle the array. Then just get each pair of sequential elements.

var names = ['Ben', 'Mike', 'Sally', 'Joe', 'Bob', 'Sam', 'Carl', 'Frank']
//var to pick two random names from an array
function pickTwo(arr) {
  let result = [];
  while (arr.length > 0) {
    let random = Math.floor(Math.random() * arr.length);
    let random2 = Math.floor(Math.random() * arr.length);
    while (random === random2) {
      random2 = Math.floor(Math.random() * arr.length);
    }
    //remove random and random2 from array
    //run untill there are no more names in the array
    let val1, val2;
    if (random < random2) {
      val2 = arr.splice(random2, 1)[0];
      val1 = arr.splice(random, 1)[0];
    } else {
      val1 = arr.splice(random, 1)[0];
      val2 = arr.splice(random2, 1)[0];
    }
    result.push([val1, val2]);
  }
  return result;
}
console.log(pickTwo(names));
console.log(names);

about 4 years ago · Juan Pablo Isaza Report

0

Some issues in your code:

  • When random and random2 are such that random + 1 == random2 and random2 == arr.length - 1, then the second splice will have no effect, since the array size was already reduced.

  • As you return in the body of the while loop, there will be never a second iteration of that loop.

I would suggest to take a different approach:

  • Randomly shuffle the array
  • Return the consecutive pairs in that shuffled array

function shuffle(a) {
    var j, x, i;
    for (i = a.length - 1; i > 0; i--) {
        j = Math.floor(Math.random() * (i + 1));
        x = a[i];
        a[i] = a[j];
        a[j] = x;
    }
    return a;
}

function pickTwo(arr) {
    arr = shuffle([...arr]);
    return Array.from({length: arr.length >> 1}, (_, i) =>
        arr.slice(i*2, i*2+2)
    );
}

var names = ['Ben', 'Mike', 'Sally', 'Joe', 'Bob', 'Sam', 'Carl', 'Frank'];

console.log(pickTwo(names));

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!