1
function largestOfFour(arr) {
    var newArray = [];
    for(var i =0; i <=arr.length-1; i++){
        console.log(arr[i]);
        newArray[i] = Math.max(arr[i]);
    }
    console.log(newArray);
              // You can do this!
    return newArray;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

So I do not understand why I am getting NaN even though i have provided an argument within my math.max function. In the console.log within my for loop, i get each array within the main array to display. Meaning if I use the same arr[i] within the max function, it should give me the max of that sub Array.

4
  • Do you want to find the largest in the arrays? Commented Aug 6, 2015 at 23:13
  • yes So what i am trying to accomplish is go through each subarray and find the max integer within it, and pass the max integer into a seperate array. So the function return an array with 4 integers each integer being the max integer of the 4 sub array within the main array. Commented Aug 6, 2015 at 23:16
  • possible duplicate of JavaScript: min & max Array values? Commented Aug 7, 2015 at 0:44
  • Possible duplicate of why math.max() returning NaN with array of integers? Commented May 14, 2018 at 16:03

5 Answers 5

3

You are passing an array to Math.max and expect it to return the maximum in that array.

However, Math.max returns the maximum among its arguments. So use

var newArray = [];
for(var i =0; i < arr.length; ++i)
  newArray[i] = Math.max.apply(void 0, arr[i]);

In ES6, you can use arrow functions and the spread operator to simplify:

arr.map(a => Math.max(...a));
Sign up to request clarification or add additional context in comments.

3 Comments

Cool! Your solution works however before I use it, i want to know what does the void 0 mean within math.max.apply()?
@AliAslam It's the value used as the this value inside the function. Since Math.max doesn't use it, it doesn't matter.
Or just newArray = arr.map(Math.max.apply.bind(Math.max, Math)) :)
2

It won't work like that. Math.max expect separate numbers. You can get desired output using apply

function largestOfFour(arr) {
    return arr.map(function(subArr){
        return Math.max.apply(Math, subArr);
    });
}

Comments

0

If you want the ultimate max do this.

var original = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]];
Math.max.apply(null, original.map(function(arr) { return Math.max.apply(null, arr); }));

or if you want a the max of each array just do

original.map(function(arr) { return Math.max.apply(null, arr); });

1 Comment

So this actually returns a single max among all arrays which is not the OP's intent. But can be a separate task
0

Using lodash

var x = ([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
_.flatten(x).max();

Comments

-1

Edited: In order to add values to the array, I'd suggest using the push methodn. As in: NewArray.push(Math.max(...sub[i])); or newarray.push(math.max.apply(null, sub[i]). An alternative is alocating, when declarating the array, the size it will take: Var newArray = new Array(arr.length); There's this new spread operator (...) in javascript !

3 Comments

Math.max([1,2,3]) doesn't find the max aomng array elements. It returns NaN.
Yes! In fact, i've done a better research and Math.max only accepts numbers, not the array. If you pass sub[i] you will have the NaN. I suggest newarray.push(null, math.max.apply(sub[i]))
There is no spread operator (...) in widely available javascript, i.e. in browsers. As mentioned in other answers it is a feature of ES6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.