As I populate an array from input fields, I am faced with the task of finding the largest number in that array.
Using Math.max(myData) results in a NaN error, and relying on an "if" statement sometimes gives correct results and sometimes doesn't. For instance, when the array contains 40 and 100, it incorrectly identifies 40 as the larger number, but it works fine with 500.
To ensure Math.max functions correctly, do I need to create a new function that converts strings into numbers?
Here is my code, so you can pinpoint the error:
function Data() {
var h = 0;
var secnd = 1;
var najBr = 0;
for (var i = 0; i < valGrup2; i++)
{
var first = 1;
myDataName[i] = document.getElementById('ime' + secnd).value;
for (var j = 0; j < val2; j++)
{
myData[h] = document.getElementById("inputpolja" + first + secnd).value;
if(myData[h]>najBr){
najBr=myData[h];
}
myDataValue[h] = document.getElementById("inputpolja" + first + secnd).value;
h++;
first++;
}
secnd++;
}
//najBr=Math.max(myData);
console.log(najBr);