0

Here is my current code:

var largestNumbers = new Array(4);

function largestOfFour(arr) {
    var biggest = Math.max.apply(null, largestOfFour) 
    for (var index in arr) {
        for(var i=0; i < arr[index].length; i++){
            if (arr[index] > biggest){
                arr[index].push(largestNumbers);
            }
            else if (arr[index] < biggest) {
                arr[index].splice(0, 1);
            }
        }
    }
    return largestNumbers;
}

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

My objective is to find the largest number in each of the subarrays and then put those values into another array and then return that value. I have been searching around for things like "How to extract a subarray from array" and that has led to me using the splice function. I'm not sure if I'm using it right, but I'm trying everything I've got because I've been stuck on this problem for 2 days now. If anyone could just lead me in the path to solve this problem, I would be more than grateful. Thank you all so much.

Furthermore, I was using this site to try and guide me along. There is a snippet of code that attempts to resolve a problem similar to mine in the latter half of the page. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply

Backs
  • 24,430
  • 5
  • 58
  • 85
Chasen Bettinger
  • 7,194
  • 2
  • 14
  • 30

8 Answers8

4
var largest = function largest(arr) {
    return arr.map(function(arr) {
        return Math.max.apply(null, arr);
    });
};

Working example

Jeff Shaver
  • 3,315
  • 18
  • 19
1
  • Use map to create a new Array by looping over the first level.
  • Use Math.max to get the maximum value from a list of numbers
  • Use apply to convert the array to arguments in Math.max

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

var myArray = [
  [4, 5, 1, 3],
  [13, 27, 18, 26],
  [32, 35, 37, 39],
  [1000, 1001, 857, 1]
];
console.log(largestOfFour(myArray));
epascarello
  • 204,599
  • 20
  • 195
  • 236
1

Applying simple nested loop's logic.

function largestOfFour(arr) {

var maxArray = [];

for(var i = 0; i < arr.length; i++) {

var maxE = 0;

   for(var j = 0; j < arr.length; j++) {

      if(maxE < arr[i][j]) {

      maxE = arr[i][j];

   }

 }

   maxArray.push(maxE);

 }
  arr = maxArray;
  return arr;

 }

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

OR Try this

function largestOfFour(arr) {

var maxArray = [];


for(var i = 0; i < arr.length; i++) {

 var maxE = Math.max(...arr[i]);          // this spread is only available in ES6 so can be  replaced by --> Math.max.apply(null, arr[i])

  maxArray.push(maxE);
}

arr = maxArray;

return arr;

}


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

Just do it with a simple nested loop. Find the biggest number for each inner array and add it to the result:

function largestOfFour(arr) {
    var result = [];

    for(var i = 0; i < arr.length; i++){
        var max = 0;
        var inner = arr[i];
        for(var j = 0; j < inner.length; j++){
            if(inner[j] > max)
                max = inner[j];
        }
        result.push(max);
    }

    return result;
}

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

Here is a working example

musefan
  • 47,875
  • 21
  • 135
  • 185
0
function largestOfFour(arr) {
    return arr.reduce(function(p, c, index, arr){
        p.push(Math.max.apply(null, c));
        return p;
    }, []);
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Jones
  • 1,480
  • 19
  • 34
Alik
  • 5
  • 2
0

Even you can code like this also , creating a new array showing in them also

function largestOfFour(arr) {
  // You can do this!
  var newArray=[];
  for(i=0;i<arr.length;i++){
    newArray.push(Math.max.apply(Math,arr[i]));

  }
  return newArray;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Sujil Anto
  • 1
  • 1
  • 2
-1
var largestNumbers = new Array();

function largestOfFour(arr) {
    for (var i = 0 ; i< arr.length; i ++) {
        largestNumbers.push(Math.max.apply(Math, arr[i]));   
    }
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
  • I wouldn't recommend a function that updates a global variable. Would be better for it to return the result, or modify an argument passed in. Still, it _does_ work so can't downvote it – musefan Jul 24 '15 at 14:09
-1

Using the approach with Math.max.apply() you can do that with one loop that iterates through the array as argument;

function largestOfFour(arr){
   var result = [];

  for(var i = 0 ; i < arr.length ; i++){
    result[i] = Math.max.apply(null, arr[i]);
  }

  return result; 
} 

EDIT: As @Jeff Shaver posted a simpler solution, you should use the map function to return the result array.

Community
  • 1
  • 1
David Rego
  • 159
  • 1
  • 5