Return Largest Numbers in Arrays kindly assist

Tell us what’s happening:

Your code so far

function largestOfFour(arr) {
  var largestNumber= [0,0,0,0];

 for(var i = 0; i < arr.length; i++){
  for (var sb= 0; sb < arr[i].length; sb++){ 
  if(arr[i][sb] > largestNumber[i]){ 
       largestNumber[i] = arr[i][sb];


  return largestNumber;

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0.

Link to the challenge:

I am getting this error.

Why did you choose this specific array? If you were to apply your function to an array of arrays of negative numbers only, you’d be in for a surprise.

Also, you should keep in mind that this function is available.

1 Like

She just used it as a placeholder, it gets modified instantly during the loop.

Note: You’ll have to calculate the sum of values in each subarray and find the largest. For example [[0, 5, 5, 0], [0, 10, 10, 5]] would mean the [0, 10, 10, 5] is the correct answer as it sums up to 25, while the first one sums up to 10.

Try to recreate these steps:

  1. Take each subarray and sum their values
  2. Place those sums in a new array
  3. Find the index number of the highest value in that new array
  4. That index number will also represent the index of a subarray with largest sum of values. That’s what you need to return.
1 Like

Unless there is an array of negative numbers only, which is the case with the test her code is failing.

1 Like

You might be right. I think I’ve missed something in her posts, will look into it later! :slight_smile:

1 Like

Your logic assumes that the greatest number in the array will be larger than 0. What about an array [-75,-20,-25,-30]? One of those is the greatest number in that array but none are every going to be > largestNumber[i].

Hint: Your for loop iterates over that array. On the first iteration -75 would be the greatest number tested so far

1 Like