Created
May 25, 2011 03:09
-
-
Save nathansmith/990244 to your computer and use it in GitHub Desktop.
Find min/max of array or multiple params.
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
| // Takes either an array or comma separated | |
| // parameters. Both can be called two ways: | |
| // | |
| // min(1, 2, 3, 4, 5); | |
| // max(1, 2, 3, 4, 5); | |
| // | |
| // OR: | |
| // | |
| // min([1, 2, 3, 4, 5]); | |
| // max([1, 2, 3, 4, 5]); | |
| function min() { | |
| if (Object.prototype.toString.call(arguments[0]) === '[object Array]') { | |
| return Math.min.apply(null, arguments[0]); | |
| } | |
| else { | |
| return Math.min.apply(null, arguments); | |
| } | |
| } | |
| function max() { | |
| if (Object.prototype.toString.call(arguments[0]) === '[object Array]') { | |
| return Math.max.apply(null, arguments[0]); | |
| } | |
| else { | |
| return Math.max.apply(null, arguments); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment