Created
June 8, 2010 17:46
-
-
Save sidonath/430390 to your computer and use it in GitHub Desktop.
This file contains 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
// 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