0

How can I avoid nan?

Array.prototype.exe=()=>{ return(Math.max(this)); } let arr =[3,6,9]; console.log(arr.exe());

11th Sep 2019, 1:57 PM
H mi
H mi - avatar
3 Antworten
+ 1
Please refrain from inputting code like this. The problem is, that you don't apply Math.max to the elements of the arraa, but the array itself, which is not a number. There are two ways to solve this: ES6+: Array.prototype.exe = function() { return Math.max(...this); }; Pre-ES6: Array.prototype.exe = function() { return Math.max.apply(this, this); };
11th Sep 2019, 2:03 PM
Airree
Airree - avatar
0
you check if the 'number' you have currently is NaN with the method isNaN since NaN mostly occurs from user input you just alert that the input is invalid and return out of the function
11th Sep 2019, 2:03 PM
Anton Böhler
Anton Böhler - avatar