Skip to content

Instantly share code, notes, and snippets.

@ClaudeSutterlin
Created April 10, 2014 02:33
Show Gist options
  • Select an option

  • Save ClaudeSutterlin/10338127 to your computer and use it in GitHub Desktop.

Select an option

Save ClaudeSutterlin/10338127 to your computer and use it in GitHub Desktop.

Revisions

  1. ClaudeSutterlin created this gist Apr 10, 2014.
    78 changes: 78 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    // gets all unique pairs
    var getPairs = function(people){
    var pairs = [];

    for (var i = 0; i < people.length; i++){
    for (var k = i+1; k < people.length; k++){
    var pair = new Pair();
    pair.person1 = people[i];
    pair.person2 = people[k];

    pairs.push(pair);
    }
    }

    return pairs;
    }

    // gets all unique matches
    var getMatches = function(pairs){
    var matches = [];

    for (var i = 0; i < pairs.length; i++){
    for (var k = i+1; k < pairs.length; k++){
    // make sure no one is on both teams
    if ((pairs[i].person1 != pairs[k].person1) &&
    (pairs[i].person1 != pairs[k].person2) &&
    (pairs[i].person2 != pairs[k].person1) &&
    (pairs[i].person2 != pairs[k].person2)){

    var match = new Match();
    match.pair1 = pairs[i];
    match.pair2 = pairs[k];

    matches.push(match);
    }
    }
    }

    return matches;
    }

    // Pair object holds two different people
    var Pair = function(){
    var person1;
    var person2;
    }

    // Match object holds two pairs
    var Match = function(){
    var pair1;
    var pair2;
    }

    /*
    Below is the actual usage of the above code
    */

    var people = ['Joe', 'Ben', 'Claude', 'Dan'];

    // get all unique pairs
    var pairs = getPairs(people);

    // display the teams
    for (var i = 0; i < pairs.length; i++){
    console.log('Team ' + i + ': ' + pairs[i].person1 + ' and ' + pairs[i].person2);
    }

    // get the matches of teams
    var matches = getMatches(pairs);

    // display the matches
    console.log('-Matches-');
    for (var i = 0; i < matches.length; i++){
    var pair1 = matches[i].pair1;
    var pair2 = matches[i].pair2;

    console.log(pair1.person1 + ' and ' + pair1.person2 + ' vs ' + pair2.person1 + ' and ' + pair2.person2);
    }