Created
April 10, 2014 02:33
-
-
Save ClaudeSutterlin/10338127 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment