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.

Dalorzo
  • 19,834
  • 7
  • 55
  • 102
Ali Aslam
  • 115
  • 1
  • 9
  • Do you want to find the largest in the arrays? – Jerome Anthony Aug 06 '15 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. – Ali Aslam Aug 06 '15 at 23:16
  • possible duplicate of [JavaScript: min & max Array values?](http://stackoverflow.com/questions/1669190/javascript-min-max-array-values) – Dalorzo Aug 07 '15 at 00:44
  • Possible duplicate of [why math.max() returning NaN with array of integers?](https://stackoverflow.com/questions/32647149/why-math-max-returning-nan-with-array-of-integers) – user202729 May 14 '18 at 16:03

5 Answers5

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));
Oriol
  • 274,082
  • 63
  • 437
  • 513
  • Cool! Your solution works however before I use it, i want to know what does the void 0 mean within math.max.apply()? – Ali Aslam Aug 06 '15 at 23:22
  • @AliAslam It's the value used as the `this` value inside the function. Since `Math.max` doesn't use it, it doesn't matter. – Oriol Aug 06 '15 at 23:29
  • Or just `newArray = arr.map(Math.max.apply.bind(Math.max, Math))` :) – Pointy Aug 06 '15 at 23:31
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);
    });
}
Kirill Slatin
  • 6,085
  • 3
  • 18
  • 38
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); });
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
0

Using lodash

var x = ([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
_.flatten(x).max();
Jerome Anthony
  • 7,823
  • 2
  • 40
  • 31
-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 !

  • `Math.max([1,2,3])` doesn't find the max aomng array elements. It returns `NaN`. – Kirill Slatin Aug 06 '15 at 23:23
  • 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])) – Pastel Belem Aug 06 '15 at 23:26
  • There is no spread operator (`...`) in widely available javascript, i.e. in browsers. As mentioned in other answers it is a feature of ES6 – Kirill Slatin Aug 07 '15 at 00:34