Question about Return Largest Numbers in Arrays

I can’t understand the line “results[i] = largestNumber”, anybody can help me with this? Thanks!

and the question goes like this:
Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.

Remember, you can iterate through an array with a simple for loop, and access each member with array syntax arr[i] .

  **Your code so far**

function largestOfFour(arr) {
const results = [];
for(let i = 0; i < arr.length; i++){
  let largestNumber =arr[i][0];
  for (let j = 1; j < arr[i].length; j++) {
    if (arr[i][j] > largestNumber) {
      largestNumber = arr[i][j];
  results[i] = largestNumber;
return results;

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

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

Hello there!

Did you try to debug this? I often just copy a set code example and try to pass through the code line by line (I use VS code and debug within it). This code just passes through the array, and at the very end (the line you are asking about) of the loop assigns the largest number to the array. In the first position results[i] is result[0] which gets the 5. Then result[1] is 27 and so forth.

This is how the debugging looks in my VScode.

The line:

let largestNumber = arr[i][0]

Is setting the variable largestNumber to be equal to the 1st value in each sub-array. The nested for loop is comparing the rest of the elements in the sub-array to the 1st one which is the largest number. So if any are larger, then that variable gets updated. If the 1st element is the largest, then it does not.

results[i] = largest number; is adding the largest number to the empty array at each index - it is assigning values to the empty array.

1 Like

Thanks, I think what you said is the answer.