Math.max = NAN ?

hI guys am trying to get the math.max of this script.


i need to create a lot of new variable dinamic for 


Objetposseder = [];


Objetposseder1, Objetposseder2, Objetposseder3, Objetposseder.....


And all variable have his own valur stored.


How i can found the variable (Objetposseder....) have the most higth valur. ???


Pid = 100; // picture ID for clic, 100 + max 36 Max 200
ItemtotalDB = 200;
Itemtotalposs = 0;
Objetposseder = [];
NBitempagemenu = 0;

for ( Listi=1; Listi < ItemtotalDB; Listi++) {
if ($gameParty.hasItem($dataItems[Listi]) == true){
Objetposseder[Listi] = Listi;//create based on DB
Itemtotalposs++;
console.log (Listi);
}
else {
Objetposseder[Listi] = 0;
}
}
console.log (Math.max(Objetposseder[]));




Have try this


Math.max(Objetposseder[]);
Math.max(Objetposseder[Listi]);






Tank you for your help guys
Hi,


As far as I know, arrays in javascript start at index 0. You start initializing the array at index 1, so you probably have an entry in your array that is probably either nil or undefined that Math.max cannot handle.


Didn't work with arrays in javascript yet though.
Hi,


As far as I know, arrays in javascript start at index 0. You start initializing the array at index 1, so you probably have an entry in your array that is probably either nil or undefined that Math.max cannot handle.


Didn't work with arrays in javascript yet though.





more Example


i have this


Objetposseder[1] = 2;


Objetposseder[2] = 4;


Objetposseder[3] = 17;


Objetposseder[4] = 14;


I need to found what Objetposseder[?]


have the most higth valur
Uh, forgot one thing:
I'm not sure if  Math.max(Objetposseder[])  is valid javascript syntax.


In case it is not, you may try  Math.max.apply(this, Objetposseder)  instead.


As said in the previous post, you have to remove non-numeric values in order to get a useful result.


In this case it would probably be the easiest to start filling your array at index 0 instead of 1.
Math.max doesn't take an array. Instead, it just takes an arbitrary number of arguments


https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max


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




Will return the largest value of the specified arguments.


To operate on an array, you can use the apply method as shown in the example:

Code:
var numbers = [1, 2, 3, ... ];
Math.max.apply(null, numbers);

Original thread: https://forums.rpgmakerweb.com/threads/math-max-nan.65079/