Skip to content Skip to sidebar Skip to footer

Why Math.max(array Int[]) Returns Nan With Typescript Even Though All Are Integers?

I am generating an array of int with the following code, this.values = new Array(3).fill(0).map((v, i) => i + 1); and then when i do, this.quantity = Math.max(this.values);

Solution 1:

Math.max does not take an array, it takes multiple arguments. (Your array is cast to a number, which leads to NaN since "1,2,3" is not a number).

You can use spread syntax for the call:

this.quantity = Math.max(...this.values);

Solution 2:

Math.max does not take an array, try this

Math.max.apply(Math, this.values);

Solution 3:

The function Math.max() does not work like that. It takes two (or more) integers and then returns the greater one. For example:

Math.max(3, 1, 2); // returns 3

Post a Comment for "Why Math.max(array Int[]) Returns Nan With Typescript Even Though All Are Integers?"