0

I'm working on a small project and trying to figure this out. Below is my code. Obviously this isn't accounting for duplicates. I'm struggling with how to account for this. Any recommendations?

When I said duplicates I meant that Joe couldn't pair with Mark then also Matt and Joe be a pair.

var people = ["Joe", "Amy", "Garrett", "Mark", "Matt", "Bri", "Rithy", "Rob", "Sandro", "Sharmila"];

for (i = 0; i < 5; i++) { 
 var pick1 = Math.floor(Math.random()*11);
 var pick2 = Math.floor(Math.random()*11);

  while (pick1 === pick2) {
    pick2 = Math.floor(Math.random()*11);
  }
  console.log(people[pick1] + " and " + people[pick2] + " are a 
  group!");
} 
GarrettJMU
  • 175
  • 1
  • 2
  • 12

5 Answers5

0

It looks like you are already checking for duplicates, but I personally wouldn't use a while loop. I would put the for loop in a function and recall the function if there is a duplicate, like this:

var people = ["Joe", "Amy", "Garrett", "Mark", "Matt", "Bri", "Rithy", "Rob", "Sandro", "Sharmila"];

var pickTwo = function() {

for (i = 0; i < 5; i++) { 
 var pick1 = Math.floor(Math.random()*11);
 var pick2 = Math.floor(Math.random()*11);

  if (pick1 === pick2) {
    pickTwo();
  } else {
  console.log(people[pick1] + " and " + people[pick2] +  "are a group!");
} 
}
}

pickTwo();

That way, the function will keep picking until it gets a pair with no duplicates. This could result in the function being called many times, but the chances that it would continue for a super long time are low.

Anthony C
  • 71
  • 3
  • I'm getting undefined cases in this piece of code. Why would that be? Also checking for duplicates I meant on any given day Amy can't be partners with Garrett and Amy be partners with Matt. If that makes sense – GarrettJMU Jul 19 '17 at 18:25
  • @GarrettJMU Oh sorry, didn't realize that – Anthony C Jul 19 '17 at 18:55
0

Try this snippet, it ensures unique partners.

  var people = ["Joe", "Amy", "Garrett", "Mark", "Matt", "Bri", "Rithy", "Rob", "Sandro", "Sharmila"];

    for (i = 0; i < people.length/2; i++) { 

      alert(people.splice(Math.floor(Math.random()*people.length),1) + " and " + people.splice(Math.floor(Math.random()*people.length),1) + " are a group!");
    } 
Joey Pinto
  • 1,735
  • 1
  • 18
  • 34
  • I'm still getting duplicates on this one. Perhaps I need to update the question. For example Matt can't be partners with Mark on the same day that Matt is a partner with Sandro. – GarrettJMU Jul 19 '17 at 18:26
  • I am sure there are no duplicates here. What you are asking isn't mentioned in the question,please upvote my answer if it solves the question the way its worded – Joey Pinto Jul 19 '17 at 18:28
  • Post it as a separate question if you wish – Joey Pinto Jul 19 '17 at 18:29
0

I don't understand what you mean by avoiding duplicates, as your code can't get a duplicate entry for pick1 and pick2.

But what I can suggest is to use a do..while loop instead of using while loop after initializing pick2 variable, your code will be like this:

  var pick2;
  do {
    pick2 = Math.floor(Math.random() * 10);
  } while (pick1 === pick2);

Demo:

var people = ["Joe", "Amy", "Garrett", "Mark", "Matt", "Bri", "Rithy", "Rob", "Sandro", "Sharmila"];

for (i = 0; i < 5; i++) {
  var pick1 = Math.floor(Math.random() * 10);
  var pick2;
  do {
    pick2 = Math.floor(Math.random() * 10);
  } while (pick1 === pick2);
  console.log(people[pick1] + " and " + people[pick2] + " are a group!");
}
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
0

My approach to this would be to remove the people that you have used from the array to prevent them from showing up again, something like this:

var people = [
 "Joe",
    "Amy",
    "Garrett",
    "Mark",
    "Matt",
    "Bri",
    "Rithy",
    "Rob",
    "Sandro",
    "Sharmila"
 ];

var num_people = people.length;
rst = {};

while ( num_people > 1 ){
 [1,2].forEach( function( num ){
     var index =  Math.floor(Math.random() * num_people);
     rst[ num ] = people[ index ];
        people.splice( index, 1 );
     num_people--;
    });
 console.log(rst[ 1 ] + " and " + rst[ 2 ] + " are a group!");
}
sauntimo
  • 1,531
  • 1
  • 17
  • 28
0

Shuffle the array, and then split to pairs:

var people = ["Joe", "Amy", "Garrett", "Mark", "Matt", "Bri", "Rithy", "Rob", "Sandro", "Sharmila"];

// returns a new shuffled array
function shuffle(array) {
  const arr = [...array];
  let m = arr.length, i;
  
  while (m) {
    i = (Math.random() * m--) >>> 0;
    [arr[m], arr[i]] = [arr[i], arr[m]]
  }
  return arr;
}

// split into arrays of partners
function chunk2(array) {
  let pairs = [];
  for(let i = 0; i < array.length; i += 2) {
    pairs.push([array[i], array[i + 1]]); // you can console.log here instead
  }
  return pairs;
}

console.log(chunk2(shuffle(people))); // shuffle and split to pairs
Ori Drori
  • 183,571
  • 29
  • 224
  • 209