Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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);

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading