-1

OK.So I came to know Math.max doesn't take array as arguments so we use apply method to find the maximum or minimum from an array as:

var numbers = [2,3,56,7,1];
var max = Math.max.apply(null,numbers); 
console.log(num); //output- 56

Is it possible to use Call method for the same question.I know Call method accepts argument list, while Apply method accepts single array as arguments.

How can we find maximum and minimum of an array using Call method?

I tried converting an array to string as:-

var s = numbers.join(", ");

But how can we give as an argument list to call method as data type of s is string?

I did:-

console.log(Math.max(null, s)); // output- NaN

which I know is wrong.

knight Viper
  • 29
  • 1
  • 2
  • 4
  • When you use `join`, the variable is of type `string` that contains comma-separated numbers, so `max` returns `NaN` – Tushar Sep 07 '15 at 04:54
  • *"Is it possible to use Call method for the same question"* <- No. The real question is why would you want to? – Phil Sep 07 '15 at 04:55
  • I am still trying understand the question :-) – Thangadurai Sep 07 '15 at 04:56
  • FYI ~ With ES6 you can use the [spread operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator) – Phil Sep 07 '15 at 04:57
  • `console.log(max);` , not num – dandavis Sep 07 '15 at 05:00
  • 1
    The whole point of using `apply` is that it accepts arguments as an array. So the reason to use `apply` is to be able to pass an array, and not the actual intended use of `apply`, which is to be able to define the value of `this`. `Math.max.apply(null, [1, 2, 3]);` is the same as doing `Math.max(1, 2, 3);`, which is the point of using it. So no, you can't use `call`, because `call` takes arguments individually and not as an array. – Jan Sep 07 '15 at 05:01
  • Or rather, you CAN use `call` but not in the same way and there's really no point at all in doing it that way. It's the same as just using `Math.max` directly (except that you can specify the value of `this`). – Jan Sep 07 '15 at 05:08

2 Answers2

1

No, you can't really solve that problem with .call(). It just doesn't do what you want.

.call() requires you to pass each argument to the host function individually. So, you could only use Math.max() to find the maximum value in an array if you knew ahead of time exactly how many items would be in the array or if you coded for all possible lengths. Basically, the situation where the arguments are in an arbitrary length array is what .apply() is built for, not what .call() is built for.

Further, if you can manually pass the arguments as .call() requires and you aren't trying to set a custom this value, then there's really no reason to use .call() at all, you may as well just call the function directly without .call(). So, I can't find any reason to use .call() for this particular problem.

Here's some more info written about .call() and .apply() from a couple days ago, including info about using .apply() with Math.max() for this particular issue.


To look at your specific example:

var numbers = [2,3,56,7,1];
var max = Math.max.apply(null,numbers); 
console.log(max); //output 56

If you wanted to not use .apply(), then you could do either of these:

var numbers = [2,3,56,7,1];
var max = Math.max(2,3,56,7,1);

or

var numbers = [2,3,56,7,1];
var max = Math.max.call(Math, 2,3,56,7,1);

But, as you can see the .call() example adds no value at all over just calling the function directly because you aren't trying to set a custom this value AND both examples require explicitly naming each argument so they don't work with an array whose length is not known ahead of time when you write the code. This example is built exactly for .apply(), not for .call(). This is why we have both options because they are built for different situations.


Note that with ES6, you can use the spread operator which is a shorthand for passing arguments like .apply() does without the custom this value:

var numbers = [2,3,56,7,1];
var max = Math.max(...numbers);

And it will solve the problem for you.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

Technically, the following would solve the problem as stated, but there is very little point to doing this. Perhaps this was some homework problem or similar?

function findMax(arr) {
    var max = Number.MIN_VALUE;
    for (var i = 0, len = (arr || []).length; i < len; i++) {
        max = Math.max.call(Math, max, arr[i]);
    }
    return max;
}
GregL
  • 37,147
  • 8
  • 62
  • 67