Skip to content

Instantly share code, notes, and snippets.

@tstone
Created March 1, 2012 16:07
Show Gist options
  • Save tstone/1950861 to your computer and use it in GitHub Desktop.
Save tstone/1950861 to your computer and use it in GitHub Desktop.
function choice(set, choice) {
return set.indexOf(choice) > -1 ? choice : '';
}
function blankArray(l) {
return (new Array(l)).map(function(){ return ''; });
}
Array.prototype.pairChoice = function(c) {
return this.pairs().map(function(pair) { return choice(pair, c); });
};
Array.prototype.elevate = function(team, step) {
var set = this
, i = set.indexOf(team);
if (typeof step === 'undefined') { step = set.length - 1; }
var loop = function(acc, set) {
var beyond = (acc + 1) > step
, l = set[acc].length;
if (l == 2) { return set.ipush(beyond ? blankArray(1) : [team]); }
return loop(acc + 1, set.ipush(beyond ? blankArray(l / 2) : set[acc].pairChoice(team) ));
};
return loop(0, [set]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment