0

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:

  1. Why is option1 returning a NaN, if I have previously used a parseInt everytime a number is stored in the array?

  2. What do the 'Math' and 'null' inside the parenthesis represent or do in option2 and option3 respectively?

  3. 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>
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Guiri
  • 79
  • 10
  • Read about [function.prototype.apply()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply). That first argument determines the `this` value when your function is evaluated, but you aren't using it so it doesn't matter what you pass as the first parameter. – Dave Dec 16 '14 at 18:19
  • You are creating an array with one element, the contents of the one input field. It's probably not what you actually want. – JJJ Dec 16 '14 at 18:19

1 Answers1

3

1) Why is option1 returning a NaN, if I have previously used a parseInt every time a number is stored in the array?

Because Math.max takes several Numbers and you're passing it an array.

2) What do the Math and null inside the parenthesis represent or do in option2 and option3 respectively?

apply takes a this parameter, which is not used inside the Math.max function, so passing null or Math (or probably anything else) works.

I would use the version passing Math. Reason being that if you supply null, the global object (window in the case of a browser) is used instead. Passing Math is a bit more explicit.

As a sidenote, options 2 and 3 are both pretty standard ways to find the maximum value of an array.

3) Why is the actual number missing in the output message? (sorry for this one being off-topic)

Because you need to concatenate the result with your message string:

alert("The highest number in the array is " + b); 
Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
  • "Because Math.max takes a Number and you're passing it an array." : Math.max takes Numbers, not just one. – Thierry Dec 16 '14 at 18:20
  • @Thierry: Yup, updated it almost right after I posted :) – Andrew Whitaker Dec 16 '14 at 18:20
  • I feel embarrassed to have forgotten about the need for concatenation for my 3rd question. Anyway, thanks for the answers !! – Guiri Dec 16 '14 at 18:38
  • Math.max takes numbers but isn't my array equal to several numbers? or maybe what you mean is that with Math.max you have to explicitly specify numbers rather than telling it "hey, look in here for the numbers"? – Guiri Dec 16 '14 at 18:53
  • @Guiri: Right, an array is a distinct data type that in your case *contains* numbers, and the `Max` method accepts several parameters of type `Number`. – Andrew Whitaker Dec 16 '14 at 18:59