My array with the largest number from each sub array isnt returning

Tell us what’s happening:
Describe your issue in detail here.
My array with the largest number from each sub array isnt returning

  **Your code so far**

function largestOfFour(arr) {
//loop through the first array and for each array in that one pull out the largest number in each array and return them as an array
let largestArr = []

for(let i = 0; i < arr.length; i++){
  for(let x = 0; x < arr[i].length; x++){
    let num = arr[x][0];
    if(num > arr[x]){
      num = arr[x]

  return largestArr;


console.log(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/100.0.4896.127 Safari/537.36

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

I’ve only just finished this course a few weeks ago but I’ll do my best to explain.

The input array you get from the assignment contains 4 arrays which each contain numbers. It’s a 2D array so you use a variable i to loop through the array of arrays and a variable x to loop through the numbers in those arrays. These two loops create one big loop that goes through the whole 2D array.

This is where the first mistake comes in- you use arr[ x ][ 0 ] to access the current number but that’s just going to give you the first number of each array. Since on the second dimension you stay at the first number (index 0).

If you want to access the current number looping through the 2D array you need to use arr[ i ][ x ]

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