Okiemute Gold
Okiemute Gold

Reputation: 548

Returning an Array of Largest Number in sub-arrays of an Array

I am trying to get the largest number of each sub-array in the array I sorted them but I can't seem to get my result. The result should be an array of the largest number of each sub-array.

function largestOfFour(arr) {
  for(var i = 0; i < arr.length; i+=1){
    for(var n = 0; n < arr[i].length; n+=1 ){
      arr[n].sort(function(a, b){return b-a});
    }
      console.log(arr[i][0]);
  }
  return arr[i][0];
}

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

Upvotes: 0

Views: 482

Answers (6)

Okiemute Gold
Okiemute Gold

Reputation: 548

function largestOfFour(arr) {
  const largestNumb = [];
  // loop through the array
  for(var i = 0; i < arr.length; i+=1){
  // loop through each sub-array (of grabbed array), and sort the numbers in descending order.
      arr[i].sort(function(a, b){return b-a});
      console.log(arr[i][0]);
  // save each of the first value (largest num) in an array
      largestNumb.push(arr[i][0]);
  }
  // console.log(largestNumb);
  return largestNumb;
}

Upvotes: 1

jacobkim
jacobkim

Reputation: 1090

Functions in javascript have variables sometimes. A variable saves a value to use whenever coders want. But functions have specific scopes so variables can be defined twice in a function. For example

let food = 'banana';
console.log(food) // it shows 'banana'
function digestion() {
let food = 'poop'
console.log(food) // it shows 'poop'
}
console.log(food) // it still shows 'banana'

So scopes can define values differently. But in largestOfFour function arr[i][0] seems out of for loof. Seems there is a little mistake. Without the line the function works.

function largestOfFour(arr) {
  for(var i = 0; i < arr.length; i+=1){
    for(var n = 0; n < arr[i].length; n+=1 ){
      arr[n].sort(function(a, b){return b-a});
    }
      console.log(arr[i][0]);
  }
  //return arr[i][0];
}

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

Upvotes: 0

Caleb Mabry
Caleb Mabry

Reputation: 36

You only have to loop through the array once and create a temporary array to store the largest numbers. Using Math.max() allows you to get the largest number within each array. There's no need to sort the arrays when Math.max() will already find the largest number for us.

function largestOfFour(arr) {
    let largestNumbers = []
    arr.forEach(innerArr => {
        let largestNumberOfInnerArray = (Math.max(...innerArr))
        largestNumbers.push(largestNumberOfInnerArray)
    });
    return largestNumbers
  }
   console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]));

Upvotes: 1

Sunday Ikpe
Sunday Ikpe

Reputation: 974

You could use underscore.js to achieve this:

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.12.0/underscore-min.js"></script>
<script>
    function largestOfFour(arr){
      return arr.map(a => _.max(a));
    }
</script>

Tryining

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

your result will be:

 [5, 27, 39, 1001]

Upvotes: 0

Jonathan Gray
Jonathan Gray

Reputation: 2609

function largestInArrays(... arrays) {
  return arrays.map(a => Math.max(...a));
}

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

This works because Math.max combined with the ... argument trick will return the largest numerical value within an array and .map allows you to create a new array from the old array by performing an operation on each element of the array.

Upvotes: 1

A Paul
A Paul

Reputation: 8251

Try something like this. You do not need the second loop, also create a temp array and put the values there.

function largestOfFour(arr) {
var tmparr = [];
  for(var i = 0; i < arr.length; i+=1){
    //for(var n = 0; n < arr[i].length; n+=1 ){
      arr[i].sort(function(a, b){return b-a});
   // }
      tmparr.push(arr[i][0]);
  }
  console.log(tmparr)
  return tmparr;
}

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

Upvotes: 1

Related Questions