Skip to content

Instantly share code, notes, and snippets.

@theophani
Created October 28, 2011 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save theophani/1322203 to your computer and use it in GitHub Desktop.
Save theophani/1322203 to your computer and use it in GitHub Desktop.
best is also the best of the best
var best = function (array) {
if (!array.reduce) return array;
return array.reduce(function (p, c) {
if (c.reduce) {
return (p > best(c)) ? p : best(c);
}
return (p > c) ? p : c;
}, -Infinity);
};
var equals = function (a, b) {
var equal = (a === b);
console.log(a, b, equal);
return equal;
};
equals('z', best(['a','b','z','c']));
equals(6, best(6));
equals(4, best([1,2,3,4,1]));
equals(8, best([[1,2,3,4,1],[1,2,3,8,1],[1,2,3,4,1],[1,2,3,4,1],[1,2,3,4,1]]));
equals(12, best([[1,2,3,4,1],[1,[1,12,3,4,1],3,8,1],[1,2,3,4,1],[1,2,3,4,1],[1,2,3,4,1]]));
equals(-2, best([-5,-2,-3]));
equals(20, best([[20,2,3,4,1],[1,[1,12,3,4,1],3,8,1],[1,2,3,4,1],[1,2,3,4,1],[1,2,3,4,1]]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment