Question regarding [Return Largest Numbers in Arrays]


I have a tendency of copying the challanges into VS code and solve them there, seeing errors in the console that shows line number helps alot.

However, below code seems to work in the VS code, i tried all four test cases and the function returns correct results, however it doesnt pass the tests – result is not defined,


thanks in advance and Merry Chistmas Everyone :slight_smile:

Your code so far

  function largestOfFour(arr) {
    // You can do this!
    for ( let i =0; i < arr.length; i++) {

      let highestNum = -99999;
      let result = [];

      for (let j = 0; j < arr[i].length; j++) {
        if (arr[i][j] > highestNum) {
          highestNum = arr[i][j];
      } //console.log(highestNum)
      highestNum = -99999;
     return console.log(result)
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 (Macintosh; Intel Mac OS X 10.13; rv:64.0) Gecko/20100101 Firefox/64.0.

Link to the challenge:

The code is not working because you are not returning a single array containing all largest numbers. You are console.logging the largest number at the end of every loop.

The result variable should be initiated before the first for loop starts and it should be returned at the very end of the function.

Hope this helps :wink: