15 Jan 2010 @ 12:23 AM

Hello

This post is a very straightforward one, basically it involves using javascript to calculate the lowest or highest number from either an array or from some definite variables.

One of the very useful possibilities of this is calculating the height and width of more than 3 columns and equalizing everything to the highest.

To achieve this we can use the Math.max() property and the Math.min() property.

The Math.max() Property

This property compares all the numbers presented and extracts the highest one. It can either be used as a simple stand-alone property like this:

var var1 = 20;
var var2 = 30;
var var3 = 40;
 var maximum = Math.max(var1, var2, var3);
// or like this
var maximum = Math.max(20,30,50);

Or we can set up an array and use it a number array to feed numbers to it. Such an example would look like this, however i mention that i couldnt get this example to work. I learned Javascript by learning jQuery, not the other way around.

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

var max = [1,2,3,4,5,6].max(); // 6

The Math.min() Property

This property does exactly the opposite of what Math.max() does, as in extracting the lowest number from a set array. The code is almost identical but for keeps sake here it is

var var1 = 20;
var var2 = 30;
var var3 = 40;
 var minimum = Math.min(var1, var2, var3);
// or like this
var minimum = Math.min(20,30,50);
Array.prototype.min = function () {
    return Math.min.apply(Math, this);
};

var min = [1,2,3,4,5,6].min(); // 1

Posted By: Dinulescu Alexandru Adrian

Last Edit: 15 Jan 2010 @ 12:23 AM

EmailPermalink

Responses to this post » (None)

You must be logged in to post a comment.