Possible Duplicate:
JavaScript: min & max Array values?
var test = [-42, 0, 8];
var biggest=-Infinity;
for (i=0; i<test.length;i++)
{if (test[i]>biggest)
alert('It is!');
biggest = test[i];
else
alert("It isn't!");}
alert("The biggest element is:" + biggest);
I’m trying to write a program to find the biggest number in an array but my code is not working. Any help please?
You have been bitten by the “too few braces” bug:
If you fix this, it works just fine.
Of course, you can do this much easier using
Math.Max, as this MDN documentation sample shows: