I have this HTML/Javascript document which lets the user insert a number into a text field and after clicking a button, the number is stored in an array and the text field is cleared so more numbers (as many as the user wants) can be stored in the array.
The user is also given the option to determine the highest number stored in the array by clicking another button, which should print a message saying "The highest number in the array is number".
Now, I have found 3 possible options to determine the highest number out of those stored in the array but I can only get 2 of those options to work so far because the other option returns a NaN.
My preferred option out of the 3 is the one that returns a NaN because to me it seems less complex than the other 2. I have commented out each of the 3 options, in case people want to test them. I have also commented out the output message (which is not printing what I wanted it to).
So my questions are:
Why is option1 returning a NaN, if I have previously used a parseInt everytime a number is stored in the array?
What do the 'Math' and 'null' inside the parenthesis represent or do in option2 and option3 respectively?
Why is the actual number missing in the output message? (sorry for this one being off-topic)
Here's the code (which I only test with Internet Explorer):
<html>
<head>
<script language="javascript" text="text/javascript">
var a=new Array();
var i=0;
function intoarray(){
a[i]=parseInt(document.form1.valor.value);
document.form1.valor.value="";
i++;
}
function upper(){
//option1 is my favorite because it's the shortest but results in the value of 'b' being NaN:
var b=Math.max(a);
//option2 works but I don't know what the 'Math' inside the parenthesis does:
var b=Math.max.apply(Math, a);
//option3 works but I don't know what the 'null' inside the parenthesis does::
var b=Math.max.apply(null, a);
//this is the output message I want, but it only prints "The highest value in the array is", leaving out the actual value
alert("The highest number in the array is ", b);
}
</script>
</head>
<body>
<form name="form1">
<input type="text" id="valor">
<input type="button" value="add" onClick="intoarray()">
<input type="button" value="get highest" onClick="upper()">
</form>
</body>
</html>