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);
});
}