0

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

6 Answers 6

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

While I added an upvote to this answer, you don't really need the ellipsis (...) in the function definition, you only need it in the return line. This answer is simple and efficient.
@SundayIkpe The reason the ellipsis are in the function definition is because I want all of the arguments to be treated as a single array (so the outer brackets aren't necessary when calling the function). Hence the name change from largestOfFour to largestInArrays. However looking back at the original question it seems I might have mistaken the specific wanted functionality here.
1

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

2 Comments

Thanks alot. This worked, but after i added var, like this: var largestNumbers = [];
Thanks for catching that. I've updated the post
1
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;
}

Comments

0

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]

2 Comments

Regular sort does a string comparison not a numerical comparison
Yea, I have updated with underscoreJS. That way it behaves the way you want. Its the simplest answer I could think of.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.