Build the Largest Number Finder - Test 4

Tell us what’s happening:

Hello! I’ve tried a few approaches to solving this problem and for some reason the simplest version of code that I could come up with that uses loops still isn’t providing the correct answer. Can someone please help me identify what I might be doing wrong? Thanks!

Your code so far

function largestOfAll(arrayOfArrays) {
  let results = []

  for (let i = 0; i < arrayOfArrays.length; i++) {
    let largestNumber = arrayOfArrays[i][0];
    for (let j = 1; j < arrayOfArrays[i].length; j++) {
      if (arrayOfArrays[i][j] > largestNumber) {
        largestNumber = arrayOfArrays[i][j];
      }
    results.push(largestNumber);
    }
  return results;
  }
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36

Challenge Information:

Build the Largest Number Finder - Build the Largest Number Finder

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-largest-number-finder/a789b3483989747d63b0e427.md at main · freeCodeCamp/freeCodeCamp · GitHub

Hi @snaylspace,

Sometimes it helps to talk through what this function should do. Can you do that? Just step it out…

You have an array of arrays and you need to return the largest number in each array, right?

Try running Tests #4-6 to compare what your code is returning to what is expected:

console.log(largestOfAll([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]));
console.log(largestOfAll([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]));
console.log(largestOfAll([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]));

Happy coding

Hi @dhess - Thanks for your repsonse. I used console.log and this is what is being output from the examples that were provided in the test steps:

[ 5, 5, 5 ]
[ 27, 27, 27 ]
[ 9, 9, 9 ]
[ 23, 25, 25 ]

I still can’t seem to figure this out even when talking through it in stages with myself. I’ve done some research and others have been able solve this type of problem with similar code to mine, so I’m wondering if there’s a syntax error I’m overlooking?

Looking at the first function call, without doing any coding, what is the largest number in each of the arrays there?

Hi @snaylspace

You’re assigning the same largest number in the first array to every other array.

You need to find the largest number in each array.

Happy coding

These are the results that would be correct, I think, based on the test cases/inputs provided:

[5,27,39,1001]
[27,5,39,1001]
[9,35,97,1000000]
[25,48,21,-3]

Also, your returned array contains one element less than expected.

Hi @Teller - Thanks for your response. How so, though? Based on the above I’m specifying the largestNumber for only individual array elements within the array (eg. arrayOfArrays[i]).

Yes, and you can verify that by looking at what the tests say the function call should return.

Now can you think of a way you can push the largest number from each array into a separate array that will be returned?

Yes, I can see that. I can’t figure out why though.

Hi @dhess - I’m trying to do that by using the results.push(largestNumber); section of code.

Get some logs in there for largestNumber so you can see what your code is doing.

Please pay close attention to where you are pushing largestNumber to results and where you are returning.

So at the moment there are two issues:

  1. the wrong number of elements are returned

  2. the same number is returned

As @dhess suggests, add some console logs for where you are making assignments.

I’d start by addressing issue one, but it is up to you.

Thanks @dhess and @Teller - I was able to solve this problem by moving the push code and the return results code to sit within different brackets, respectivley. :slight_smile: