Skip to content

Instantly share code, notes, and snippets.

@sidonath
Created June 8, 2010 17:46
Show Gist options
  • Save sidonath/430390 to your computer and use it in GitHub Desktop.
Save sidonath/430390 to your computer and use it in GitHub Desktop.
// approach one: simply assign function to prototype
// it works, but you still need to use "apply" to make the method
// work as needed
function smallest1(array){
Array.prototype.min = Math.min;
return array.min.apply(array, array);
}
// approach two: use apply in the function assigned to Array's prototype
function smallest2(array){
Array.prototype.min = function () { return Math.min.apply(Math, this); };
return array.min();
}
function largest(array){
return Math.max.apply( Math, array );
}
assert(smallest1([0, 1, 2, 3]) == 0, "Locate the smallest value.");
assert(smallest2([0, 1, 2, 3]) == 0, "Locate the smallest value.");
assert(largest([0, 1, 2, 3]) == 3, "Locate the largest value.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment