Created
March 1, 2012 19:35
-
-
Save tstone/1952508 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
var choose = function(a, b, teams) { | |
if (teams[a] && teams[b]) { | |
return teams[a] < teams[b] ? a : b; | |
} else if (teams[a]) { | |
return a; | |
} else if (teams[b]) { | |
return b; | |
} else { | |
return Math.random() < 0.5 ? a : b; | |
} | |
}; | |
Array.prototype.bracketShift = function(index, teams) { | |
var leftSet = this[index]; | |
return this.ipush(leftSet.pairs().map(function(pair) { | |
return choose(pair[0], pair[1], teams); | |
})); | |
}; | |
Array.prototype.bracket = function(teams) { | |
var l = (this.length / 2) - 1; | |
var loop = function(acc, set) { return acc === l ? set : loop(acc + 1, set.bracketShift(acc, teams)); }; | |
return loop(0, [this]); | |
}; | |
// In use | |
var teams = ['vikings', 'lions', 'bears', '49ers', 'cubs', 'seahawks', 'giants', 'seagulls']; | |
var bracket = teams.bracket({'vikings': 1, 'seagulls': 2}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment