Return Largest Numbers in Arrays -

I am stuck on this challenge. I’m not sure what I am doing wrong. It passes the first three tests but the fourth it fails. Can anyone tell me what I am missing? Thanks :slight_smile:

Your code so far


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

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

   for(var j = 0; j < arr[i].length; j++) {
      if(arr[i][j] > largest[i]) {         
          largest[i] = arr[i][j];
      }
    }
  }
  // You can do this!
  return largest;
}

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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays/

1 Like

Ah. took me a minute, but think through the logic: you’re initializing your largest array with zeroes, assuming your values will all be larger than zero.

What is happening in the last case that could break your assumptions?

1 Like

the first part makes total sense to me - the zeroes are the problem because of the negative numbers that come up - they are less than zero and so that’s going to cause an error. But I’m not sure how to solve this. Taking the zeroes out and making it an empty array passes only the first test. There’s a bit of logic here I’ve missed that I’m just not seeing, and the fact I’m not seeing it makes me wonder if I need to go back and do some remedial studying (and if so, at which point do I begin again? eeks)

thanks for your fast reply, and any additional insight you can provide :slight_smile:

1 Like

How about this?

  • Initialize largest to an empty array.
  • Inside your first loop (which iterates over the outer array), set the value of largest[i] to the first value in arr[i].
  • Run the inner loop exactly as you are.

By doing this, you know that at least the values in your largest array are accurate to the arrays, and not a meaningless collection of 0.

2 Likes

would you mind showing me what that would look like? I tried a couple of different ways inside my outer loop and I’m just not getting it. thank you so much for your help on this.

1 Like

largest[i] = arr[i][0]

Exactly like that

And you can actually avoid the comparison between the first element of the array and itself, they are the same because you are building largest like that - to make it a tiny bit more efficient you just need to change a single thing in there

1 Like

Delighted to help.

And rather than give you the answer outright, consider this – you’re already setting largest[i] = arr[i][j] inside the inner loop, right? So, just after the OUTER loop starts, do what @ILM just said.

sigh. Beats me to the punch, every time. :wink:

1 Like

Will not intervene anymore! :stuck_out_tongue:

hahaha! You did great, and you’re exactly right. Just, my old fingers ain’t so speedy as they used to be.

On this one (and in many other cases), we make a heck of a team, @ILM. Keep it up!

ahh! thank you so much!! :slight_smile:

1 Like

thank you so much for your help!!

you both completely rule - I was pulling my hair out (figuratively I mean) :slight_smile:

1 Like

May I recommend using -Infinity rather than 0. This approach only works if values > 0.

This problem is just a barrier to keep the unmotivated away.
Soon you will learn the cool way to solve these kind of problems.