If you want Math.min() to process an array of values, you have to use .apply():

var lowestTime = Math.min.apply(Math, times);

You could also use the new-ish spread syntax:

var lowestTime = Math.min(... times);

but that doesn't work in all environments.

When you pass the array directly, the first thing Math.min() does is try to convert it to a number because that's what it expects: individual numbers. When that happens, through a round-about process the resulting value will be NaN.

Answer from Pointy on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › min
Math.min() - JavaScript - MDN Web Docs
Math.min() Math.min(value1) ... the lowest value will be selected and returned. The smallest of the given numbers. Returns NaN if any of the parameters is or is converted into NaN....
Top answer
1 of 4
1

Math.min() requires the arguments to be numbers. It will convert strings to numbers, but each value needs to be in a separate argument -- it won't convert a comma-delimited string to multiple numbers.

Don't use a string for result, use an array. Then you can spread the array into function arguments.

function calculate() {
  var array1 = [1, 1, 1];
  var array2 = [
    [2, 2, 2],
    [3, 3, 3],
    [4, 4, 4]
  ];
  var i = 0;
  var result = [];
  for (; i < array2.length; i++) {
    result.push(Math.sqrt(Math.pow((array1[0] - array2[i][0]), 2) + Math.pow((array1[1] - array2[i][1]), 2) + Math.pow((array1[2] - array2[i][2]), 2)));
  };
  console.log(result);
  console.log(Math.min(1.7320508075688772, 3.4641016151377544, 5.196152422706632, ));
  var minResult = Math.min(...result);
  console.log(minResult);

}
<button onclick="calculate()">Click me</button>

2 of 4
1

You're passing as a param on Math.min a string

"1.7320508075688772,3.4641016151377544,5.196152422706632,"

You can split that string (not recommended)

function calculate() {
  var array1 = [1, 1, 1];
  var array2 = [
    [2, 2, 2],
    [3, 3, 3],
    [4, 4, 4]
  ];
  var i = 0;
  var result = '';
  for (; i < array2.length; i++) {
    result += Math.sqrt(Math.pow((array1[0] - array2[i][0]), 2) + Math.pow((array1[1] - array2[i][1]), 2) + Math.pow((array1[2] - array2[i][2]), 2)) + ',';
  };
  console.log(result);
  console.log(Math.min(1.7320508075688772, 3.4641016151377544, 5.196152422706632, ));
  var minResult = Math.min.apply(null, result.split(",").filter(n => n.trim() !== "").map(Number));
  console.log(minResult);

}
<button onclick="calculate()">Click me</button>

Or, add the numbers into an array and find the min value using the function apply as follow

function calculate() {
  var array1 = [1, 1, 1];
  var array2 = [
    [2, 2, 2],
    [3, 3, 3],
    [4, 4, 4]
  ];
  var i = 0;
  var result = [];
  for (; i < array2.length; i++) {
    result.push(Math.sqrt(Math.pow((array1[0] - array2[i][0]), 2) + Math.pow((array1[1] - array2[i][1]), 2) + Math.pow((array1[2] - array2[i][2]), 2)));
  };
  console.log(result);
  console.log(Math.min(1.7320508075688772, 3.4641016151377544, 5.196152422706632, ));
  var minResult = Math.min.apply(null, result);
  console.log(minResult);

}
<button onclick="calculate()">Click me</button>

🌐
GeeksforGeeks
geeksforgeeks.org › javascript-math-min-method
JavaScript Math min() Method | GeeksforGeeks
July 15, 2024 - The JavaScript Math min( ) Method is used to return the lowest-valued number passed in the method. The Math.min() method returns NaN if any parameter isn't a number and can't be converted into one.
🌐
Gitbook
baldur.gitbook.io › js › js-math › mathematical › math-max-method
Math max()/min() Method | JS/TS
January 18, 2021 - The result is “-Infinity” if no arguments are passed and the result is NaN if at least one of the arguments cannot be converted to a number. The max() is a static method of Math, therefore, it is always used as Math.max(), rather than as ...
🌐
TechOnTheNet
techonthenet.com › js › math_min.php
JavaScript: Math min() function
The numbers that will be evaluated to determine the smallest value. The min() function returns the smallest value from the numbers provided. If no parameters are provided, the min() function will return Infinity. If any of the parameters provided are not numbers, the min() function will return NaN.
Find elsewhere
🌐
Programiz
programiz.com › javascript › library › math › min
JavaScript Math min()
// min() with characters arguments let minNum = Math.min('A', 'B', 'C'); console.log(minNum); // Output: // NaN // NaN
🌐
O'Reilly
oreilly.com › library › view › javascript-the-definitive › 0596101996 › re117.html
Math.min( ): return the smallest argument — ECMAScript v1; enhanced in ECMAScript v3 - JavaScript: The Definitive Guide, 5th Edition [Book]
Returns Infinity if there are no arguments. Returns NaN if any argument is NaN or is a nonnumeric value that cannot be converted to a number. Get JavaScript: The Definitive Guide, 5th Edition now with the O’Reilly learning platform.
🌐
Droidscript
droidscript.org › javascript › Global_Objects › Math › min.html
Math.min
Because min() is a static method of Math, you always use it as Math.min(), rather than as a method of a Math object you created (Math is not a constructor). If no arguments are given, the result is Infinity. If at least one of arguments cannot be converted to a number, the result is NaN.
🌐
Stack Overflow
stackoverflow.com › questions › 52897952 › equations-on-numbers-return-nan
javascript - Equations on numbers return NaN - Stack Overflow
The relevant bit of code is var cEnergy = Math.min(pEnergy-pwrCons+cCharge,totalBattCap); I run Logger.log(cEnergy) and it returns NaN. So I ran Logger.log(typeof()) on cEnergy, pEnergy, pwrCons,
🌐
Reality Ripple
udn.realityripple.com › docs › Web › JavaScript › Reference › Global_Objects › Math › min
Math.min() - JavaScript - UDN Web Docs: MDN Backup
If at least one of arguments cannot be converted to a number, the result is NaN. This finds the min of x and y and assigns it to z: ... Math.min() is often used to clip a value so that it is always less than or equal to a boundary. For instance, this · var x = f(foo); if (x > boundary) { x ...
Top answer
1 of 5
127

The reason why your code doesn't work is because Math.max is expecting each parameter to be a valid number. This is indicated in the documentation as follows:

If at least one of arguments cannot be converted to a number, the result is NaN.

In your instance you are only providing 1 argument, and that 1 value is an array not a number (it doesn't go as far as checking what is in an array, it just stops at knowing it isn't a valid number).

One possible solution is to explicitly call the function by passing an array of arguments. Like so:

Math.max.apply(Math, data);

What this effectively does is the same as if you manually specified each argument without an array:

Math.max(4, 2, 6, 1, 3, 7, 5, 3);

And as you can see, each argument is now a valid number, so it will work as expected.

Spreading an array

You can also spread the array. This essentially treats the array as if each item is being passed as it's own argument.

Math.max(...data);
2 of 5
44

if you see doc for Math.max you can see next description

Because max() is a static method of Math, you always use it as Math.max(), rather than as a method of a Math object you created (Math is not a constructor).

If no arguments are given, the result is -Infinity.

If at least one of arguments cannot be converted to a number, the result is NaN.

When you call Math.max with array parameter like

Math.max([1,2,3])

you call this function with one parameter - [1,2,3] and javascript try convert it to number and get ("1,2,3" -> NaN) fail.
So result as expected - NaN

NOTE: if array with just one number - all work correctly

 Math.max([23]) // return 23

because [23] -> "23" -> 23 and covert to Number is done.


If you want get max element from array you should use apply function, like

Math.max.apply(Math,[1,2,3])

or you can use the new spread operator

Math.max(...[1,2,3])