Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Question about Math.min()

Reply
Thread Tools

Question about Math.min()

 
 
Yansky
Guest
Posts: n/a
 
      05-31-2007
Hi, just a quick question about the Math.min() function.

If I have an array of numbers:

var tempArray = [12,45,2,67,86];

how come Math.min() can't look at all the numbers in the array at
once? i.e. - If I put all of the numbers in the array like so:

Math.min(12,45,2,67,86)

I get the lowest number, but when I try this:

Math.min(tempArray)

I get an error of NaN.


Cheers.

 
Reply With Quote
 
 
 
 
Pete
Guest
Posts: n/a
 
      06-01-2007
On Jun 1, 7:49 am, Yansky <thegoodd...@gmail.com> wrote:
> Hi, just a quick question about the Math.min() function.
>
> If I have an array of numbers:
>
> var tempArray = [12,45,2,67,86];
>
> how come Math.min() can't look at all the numbers in the array at
> once? i.e. - If I put all of the numbers in the array like so:
>
> Math.min(12,45,2,67,86)
>
> I get the lowest number, but when I try this:
>
> Math.min(tempArray)
>
> I get an error of NaN.
>
> Cheers.


Math.min only accepts 2 or more numbers. It won't accept an array.

 
Reply With Quote
 
 
 
 
Douglas Crockford
Guest
Posts: n/a
 
      06-01-2007
Yansky wrote:
> If I have an array of numbers:
>
> var tempArray = [12,45,2,67,86];
>
> how come Math.min() can't look at all the numbers in the array at
> once? i.e. - If I put all of the numbers in the array like so:
>
> Math.min(12,45,2,67,86)
>
> I get the lowest number, but when I try this:
>
> Math.min(tempArray)
>
> I get an error of NaN.


Math.min.apply(Math, tempArray)

http://javascript.crockford.com/
 
Reply With Quote
 
Pete
Guest
Posts: n/a
 
      06-01-2007

>
> Math.min.apply(Math, tempArray)
>



works fine if your array doesn't have any undefined items.

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      06-01-2007
On Jun 1, 5:19 pm, Pete <peter.gal...@gmail.com> wrote:
> > Math.min.apply(Math, tempArray)

>
> works fine if your array doesn't have any undefined items.


If you have a sparse array and you know that there are no null values
(i.e. all the gaps are undefined) then copy the array, do a numeric
sort and get the value at index 0:

function asNum(a, b) { return a-b; }

function getMin(a) {
var b = a.concat()
return b.sort(asNum)[0];
}

var a = [];
a[500] = 13;
a[1000] = 0;
a[250] = 10;

alert(getMin(a));


--
Rob

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57