-3

I want to extract the largest number of every single array. I believe it should be possible with the .map function.

This is what I have so far, but unfortunately this returns " [ undefined, undefined, undefined, undefined ]"

Code:

function largestOfFour(largestOfFour) {
    largestOfFour.forEach(function(number){
        number.sort(function(a, b){
            return b - a;
        });
        highestValue = number.map(function(number){
            return number[0];
        });
    });
};

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Andy
  • 61,948
  • 13
  • 68
  • 95
Cake
  • 493
  • 7
  • 16
  • Your problem is that you have `number` as the parameter to your `forEach` and then `number` again in your `map` function. So inside your `map` function, what do you think `number` refers to (hint, it isn't an array). – Matt Burland Nov 25 '15 at 15:45
  • Use `arr.map(Math.max.apply.bind(Math.max, null));` Check [this answer](http://stackoverflow.com/a/32679435/2025923) for explanation and demo – Tushar Nov 25 '15 at 15:45

4 Answers4

2

There's a shorter way of finding the largest number in each array using Math.max:

function largestOfFour(arr) {
  return arr.map(function (el) {
      return Math.max.apply(null, el);
  });
}; // [ 5, 27, 39, 1001 ]

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95
  • There's _even_ shorter way. Use `arr.map(Math.max.apply.bind(Math.max, null));` Check [this answer](http://stackoverflow.com/a/32679435/2025923) for explanation and demo – Tushar Nov 25 '15 at 15:46
  • 1
    @Tushar, [short, clever code is not better than more verbose simple code](http://zzzzbov.com/blag/debugging-clever-code). – zzzzBov Nov 25 '15 at 15:56
  • @zzzzBov True. But once you understand the syntax(or with comments), it's good and concise code. – Tushar Nov 25 '15 at 16:01
  • @Andy I know about the Math method, but I don't understand what " apply.bind" does. Could you elaborate on that? – Cake Nov 25 '15 at 16:04
  • You should ask @Tushar – Andy Nov 25 '15 at 16:10
  • @Cake Check my first comment and read the description – Tushar Nov 25 '15 at 16:12
2

You're iterating over the outer array,

function largestOfFour(largestOfFour) {
    largestOfFour.forEach(function(number){

and then sorting the inner arrays,

number.sort(function(a, b){
    return b - a;
});

and then of those inner arrays you're trying to acquire the [0] property from each value, which is undefined

highestValue = number.map(function(number){
// you redefine `number` here to be the value within the inner array
    return number[0];
});

What you probably want to do is map the outer array:

function largestOfFour(largestOfFour) {
    largestOfFour.map(function(numbers){

sort the inner arrays:

numbers.sort(function(a, b){
    return b - a;
});

and then return the first value:

return numbers[0];

All together it probably should be:

function largestOfFour(largestOfFour) {
    return largestOfFour.map(function(numbers){
        numbers.sort(function(a, b){
            return b - a;
        });
        return numbers[0];
    });
}

Although there are simpler ways to find the maximum values of an array of arrays.

One such example is to use Array.prototype.reduce:

function largestValues(outer) {
    return outer.map(function (inner) {
        return inner.reduce(function (prev, current) {
            return Math.max(prev, current);
        }, Number.NEGATIVE_INFINITY);
    });
}
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
0

http://jsfiddle.net/y48qz6hx/ Just fixed your code

function largestOfFour(largestOfFour) {
    largestOfFour.forEach(function(number, index){
        number.sort(function(a, b){
            return b - a;
        });
        largestOfFour[index] = number[0];
    });

    alert(largestOfFour);
};

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
z1m.in
  • 1,661
  • 13
  • 19
0

try this:

var ar=[[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]];

   console.log( ar.map(x=> Math.max.apply(null, x)) )
maioman
  • 18,154
  • 4
  • 36
  • 42