Simpel wrote:
Hi there! not really a jquery question this maybe but hopefully
someone will answer it anyway...

I've got an array with different productprices. I'd like to get the
highest and lowest price out of this array. What's the best way to do
this?

/J

A little less known that is, the Math.max/Math.min functions in JavaScript can take more than two arguments, try this:

var array = [5, 7, 89, 3, 0, 67];
var max = Math.max.apply(null, [5, 7, 89, 3, 0, 67]); // => 89

Or enhance the Array object itself:

Array.prototype.max = function() {
    return Math.max.apply(null, this);
};
Array.prototype.min = function() {
    return Math.min.apply(null, this);
};

Usage:

[5, 7, 89, 3, 0, 67].max(); //  => 89


I'm not quite sure if this is implemented in IE but give that a try first!



--Klaus

Reply via email to