Reputation: 1566
Pardon my confusion ... I'm going through the entirety of W3School of JS as a quick reference to get oriented to all that is JS ...
Came across this code, and while I understand the mechanisms of it, I don't understand how it works, i.e., finding the largest number.
Any help would be appreciated on the how it works to find the largest number by comparing against the negative infinity?
Code: (link of where it came from)
<!DOCTYPE html>
<html>
<body>
<p>Finding the largest number.</p>
<p id="demo"></p>
<script>
function findMax() {
var i;
var max = -Infinity;
for(i = 0; i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i];
}
}
return max;
}
document.getElementById("demo").innerHTML = findMax(4, 5, 6);
</script>
</body>
</html>
Upvotes: 5
Views: 4260
Reputation: 50807
The answer can become clear when you try to determine how else you would do this. Here's another candidate solution:
function findMax() {
var i;
var max = arguments[0];
for (i = 1; i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i];
}
}
return max;
}
This one seems simpler. You don't need an initial value, taking instead the first value of the arguments.
And it seems to work:
findMax(8, 6, 7, 5, 3, 0, 9, 8, 6, 7, 5); //=> 9
It works even if you only supply a single value:
findMax(42); //=> 42
Can you see where it fails? There is a serious problem with this implementation.
Did you find it?
findMax(); //=> undefined
When you pass it no arguments at all, you get undefined
, a non-numerical result.
With the other version, it would return -Infinity
. This answer has a numeric type; so the function always returns a number. Having consistent return types makes programming with the system much easier.
And if you really want to understand the basic idea more deeply, imagine a concatenateAllStrings
function. You could do this much the same way, adding to your final value each new string your encounter. But you would need to start with something. There's only one reasonable choice: the empty string. That's because for any string s
, '' + s //=> s
. In your case, you want the value x
such that for any number n
, n >= x
. What's less than every number? Really only -Infinity.
For a still deeper look, you might investigate "folding over a Semigroup".
I've actually used a related fact in interviewing JS developers. This is only once the user has shown herself to be reasonably competent. I ask something like this:
This sounds a bit like a trick question, but the goal is to see how you might try to understand existing systems. It's an odd quirk of Javascript that
Math.max() < Math.min()
. Can you discuss howmax
andmin
might be implemented to make this happen?
And then I guide her through it, offering as many hints as necessary until she has explained it in full. No one has ever known the answer right off. How much guidance the candidate needs, and how well she seems to understand the explanation, helps me determine something useful about the her.
Upvotes: 0
Reputation: 32145
Any help would be appreciated on the how it works to find the largest number by comparing against the negative infinity?
The code is not always comparing to negative Infinity
here:
var max = -Infinity;
But instead it's just initializing the max
variable to -Infinity
so it guarantees that when we loop the array values, there won't be any value lower than it.
Analysis:
For example when we call findMax(4, 5, 6)
, we will have three iterations:
if(arguments[0] > max)
<==> 4>-Infinity
is true
so max=4
.if(arguments[1] > max)
<==> 5>4
is true
so max=5
.if(arguments[2] > max)
<==> 6>5
is true
so max=6
.Conclusion:
So you can see here that max
value will be always updated with the first element in the array
as any passed number will be higher than -Infinity
, even if we passed negative values like -1240000
, -9999999
or -102457933210they will always be higher than
-Infinity`.
Upvotes: 1
Reputation: 7354
-Infinity
is the neutral element for the <
operator.
This means that:
-∞ > T = false for every T
If you loop starting with -∞
, you always get the first element as maximum
.
If you want to find the maximum
using a chain (which the mathematical equivalent to a loop):
s1 = max of e1,e2
s2 = max of s1,e3
s3 = max of s2,e4
...
You can rewrite as:
s1 = max of e1,-∞
s2 = max of s1,e2
s3 = max of s2,e3
...
Here s1 = e1
.
Upvotes: 1
Reputation: 10458
function findMax() {
console.log(arguments)
var i;
var max = -Infinity;
for(i = 0; i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i];
}
console.log(i, max);
}
return max;
}
console.log(findMax(1, 2, 3, 4));
As you can see (1, 2, 3, 4)
are passed in the array [arguments]
. At each iteration if arguments[i] > max
then max = arguments[i]
Note -Infinity is the smallest number, so initially max would be initialised to -Infinity
Upvotes: 0
Reputation: 13494
The example uses -Infinity
as the absolute smallest number to begin comparing with. So, if for example, you passed it all negative numbers, and they were all relatively large negative numbers, your max function would work.
e.g. findMax(-123456, -123459, -1234832383483)
In this example, it's a good initial value to use as opposed to something like zero. In that case, zero would be the result of findMax since all the numbers from the previous example are less than zero.
Upvotes: 0