473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Question about Math.min()

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.

May 31 '07 #1
4 4411
On Jun 1, 7:49 am, Yansky <thegoodd...@gmail.comwrote:
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.

Jun 1 '07 #2
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/
Jun 1 '07 #3
>
Math.min.apply(Math, tempArray)

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

Jun 1 '07 #4
On Jun 1, 5:19 pm, Pete <peter.gal...@gmail.comwrote:
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

Jun 1 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: beguigne | last post by:
Below is a snippet of a crude date picking routine for a form. I am a novice at this so, I am hitting my head at why when I select the day, the onChange event gives me a blank. What am I missing?...
22
by: James H. | last post by:
Greetings! I'm new to Python and am struggling a little with "and" and "or" logic in Python. Since Python always ends up returning a value and this is a little different from C, the language I...
10
by: MS Newsgroups | last post by:
Hi, I have a feeling there is a easy answer to this. I need to return the first 9 characters in a string if it is longer than 9 characters, otherwise the whole string. If was hoping this code...
5
by: Petrakid | last post by:
Alright, so I asked earlier about this pig/chicken leg count thing and got some good help with it, thanks. I come to find out now that I do not supply just one number, but a range of numbers. ...
0
by: sean | last post by:
I'm trying to create a "rubber band" rectangle effect on my form. Private Sub HighlightSectionOfTrack(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles...
1
by: avocado12 | last post by:
Hi, I'm sure this is very basic but it's difficult to google or search for because I'm searching for ":". Can anyone explain or point me to a reference for the ":" operator used in Javascript?...
17
by: Albert Hopkins | last post by:
This issue may have been referred to in news:<mailman.1864.1196703799.13605.python-list@python.orgbut I didn't entirely understand the explanation. Basically I have this: 6.0 nan 6.0 nan ...
15
by: DL | last post by:
say, we have the following: // declare an array myArray = ; // short hand? same as myArray = new Array ? // populate it myArray = 0; myArray = 0; myArray = 1; myArray = 0;
13
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, Why does Math.Sqrt() only accept a double as a parameter? I would think it would be just as happy with a decimal (or int, or float, or ....). I can easily convert back and forth, but I am...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.