Where is the mistake

Can someone tell me what did I do here wrong?


function largestOfFour(arr) {
//Empty array to store the values
let array = [];
let largest = 1;
//Loop through the array
for (let i = 0; i < arr.length; i++) {
  for (let j = 0; j < arr[i].length; j++) {
    //condition
    if (arr[i][j] > largest) {
      largest = arr[i][j];
    }
  }
  array.push(largest);
}
return array;
}

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

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

two things

  1. You are initializing largest as 1, but at-least one of the input arrays has all negative values. This will give you issues.
  2. You are going to want to reset the value of largest after comparing a set of 4 values. The way you have it now, if your input is ([1, 2, 3, 4], [0,1,2,3]) your output will be [4,4].

Let me know if this helps.

2 Likes

DavidMatthewFraser has the main points covered, I would just hop on to add that you don’t need to initialize your largest to anything.

What you have is very, very close to working.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.