Basic Algorithm Scripting - Return Largest Numbers in Arrays

**Tell us what’s happening: i dont understand why my code work only in 4 steps vérification **
Describe your issue in detail here.

**function largestOfFour(arr) {

var maxValue = [0,0,0,0];

for (var i = 0; i < arr.length; i++) {
for(var j = 0; j<arr[i].length; j++) {
if (arr[i][j]> maxValue[i]){

        maxValue[i] = arr[i][j];
    }
}

}

return maxValue;
}

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

function largestOfFour(arr) {


var maxValue = [0,0,0,0];

  for (var i = 0; i < arr.length; i++) {
    for(var j = 0; j<arr[i].length; j++) {
        if (arr[i][j]> maxValue[i]){

            maxValue[i] = arr[i][j];
        }
    }
  }

  return maxValue;
}

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/106.0.0.0 Safari/537.36 Edg/106.0.1370.52

Challenge: Basic Algorithm Scripting - Return Largest Numbers in Arrays

Link to the challenge:

In the final test, you have this array: [-72, -3, -17, -10]
In your code, you start with a lowest number of 0 and check for numbers that are larger than that, but all of those numbers are smaller; so your code will return 0 as the largest number, when it should really return -3.

If you start with any arbitrary number as the default lowest number, you will run into this problem every time there is an array with even lower numbers. What can you do to make sure that you will always return a number in the array, no matter what numbers appear in it?

1 Like

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