Home > Software engineering >  Splicing random names from array until array is empty
Splicing random names from array until array is empty

Time:04-05

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

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

A slightly modified approach that uses a separate randomise function that accepts the array length, and the main function just pushes the results of splicing from the array in to an output array (and flattens the results because each splice will return its own array), and then returns that output array main array is empty.

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

function rnd(len) {
  return Math.floor(Math.random() * len);
}

function pickTwo(arr) {

 const out = [];

  while (arr.length > 0) {
    out.push([
      arr.splice(rnd(arr.length), 1),
      arr.splice(rnd(arr.length), 1)
    ].flat());
  }

  return out;

}

console.log(pickTwo(names));

Additional documentation

  • Related