-2

I want to log the min and max number from an array of numbers, that keep changing on runtime. I am using ES5 Math.min and Math.max. I even clone the input array to avoid concurrent changes. This is my code:

var runTimes = input.runTimes.slice(0); // clone input to avoid further changes
console.log("RUN TIMES", runTimes);
var min = Math.min(runTimes);
var max = Math.max(runTimes);
console.log("RUN TIMES MIN", min);
console.log("RUN TIMES MAX", max);

I get NaN even though all items are numbers, verified by the log: enter image description here

Stefanos Kargas
  • 10,547
  • 22
  • 76
  • 101
  • 2
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min Read the documentation's demo on its usage – epascarello Jan 11 '21 at 14:03
  • 1
    [Search results for "Math.max NaN"](https://www.google.com/search?q=math.max+nan+site%3AStackoverflow.com) – adiga Jan 11 '21 at 14:08

1 Answers1

3

Math.min or Math.max do not expect array but distinct numbers. Please try this:

var min = Math.min(...runTimes);
var max = Math.max(...runTimes);