Basic Algorithm Scripting - Return Largest Numbers in Arrays

Tell us what’s happening:
After implementing the code the largest numbers of each subarray where all displayed in a new array on the console but it didn’t still pass the test

Your code so far

function largestOfFour(arr) {
  let newArr = [];
  for (let i = 0; i < arr.length; i++) {
    let largestNumbers = arr[0][i]
    for (let j = 0; j < arr[i].length; j++) {
     if (arr[i][j] > largestNumbers) {
        largestNumbers = arr[i][j]
     }
    }
newArr.push(largestNumbers)
  }
  return newArr;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Return Largest Numbers in Arrays

Link to the challenge:

Consider test case;
([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]) should return [27, 5, 39, 1001] .

your result is
[ 27, 27, 39, 1001 ]

For the case
largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]) should return [25, 48, 21, -3]

your result is
[ 25, 48, 25, 12 ]

There is just one tiny mistake in your code. Take a look at this line:

let largestNumbers = arr[0][i]

You are always grabbing the very first array and then i inside of that array. Those should be flipped around:

let largestNumbers = arr[i][0]

So that it is always grabbing the current array at i and the first index of that current array instead!

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