Calculating the min/max value from an array in JavaScript

The native min and max functions don’t support arrays as their argument. Instead they take a variable-length argument list of numbers. Sometimes, however, you need to pick the smallest or the biggest number from an array. In this case it’s your responsibility to provide such min/max functions.

For those who can’t wait to copy and paste, scroll down to the third example, that’s what you want.

Example #1

The following snippet implements both, min and max, with some basic type checking and returning NaN if one element is not a number.

var Math1 = {
    min: function(values) {
        if(values.length == 0) {
            return NaN;
        } else if(values.length == 1) {
            var val = values.pop();
            if ( typeof val == "number" ) {
                return val;
            } else {
                return NaN;
            }
        } else {
            var val = values.pop();
            return Math.min(val, this.min(values))
        }
    },
    max: function(values) {
        if(values.length == 0) {
            return NaN;
        } else if(values.length == 1) {
            var val = values.pop();
            if ( typeof val == "number" ) {
                return val;
            } else {
                return NaN;
            }
        } else {
            var val = values.pop();
            return Math.min(val, this.max(values))
        }
    }
}

Math1.min([100,13,3,6]); // 3
Math1.max([100,13,3,6]); // 100

Example #2

The code from the first example works just fine but both functions are pretty much identical except the final min/max operation of the math library. Essentially this is simply code duplication which should always be avoided.

In this example our self-implemented min/max functions use the new function calculate and pass it the Math’s min/max function as an argument.

var Math2 = {
    min: function(values) {
        return this.calculate(values, Math.min);
    },
    max: function(values) {
        return this.calculate(values, Math.max);
    },
    calculate: function(values, operation) {
        if(values.length == 0) {
            return NaN;
        } else if(values.length == 1) {
            var val = values.pop();
            if ( typeof val == "number" ) {
                return val;
            } else {
                return NaN;
            }
        } else {
            var val = values.pop();
            return operation(val, this.calculate(values, operation))
        }
    }
}

Math2.min([100,13,3,6]); // 3
Math2.max([100,13,3,6]); // 100

Example #3

Using apply and built-in functions performs much better than the above examples. And the code is far more readable and shorter, it’s just two lines.

Math.min.apply(Math, [100,13,3,6]); // 3
Math.max.apply(Math, [100,13,3,6]); // 100

1 Comment

  1. It’s hard to find your blog in google. I found it on 19 spot, you
    should build quality backlinks , it will help you to rank to google top 10.
    I know how to help you, just search in google – k2 seo tricks

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>