Challenge: Return Largest Numbers in Arrays: Solution Not Working

Tell us what’s happening:
To complete the following challenge I wrote a code which delivered the required result but still I was not able to pass it, can someone check and let me know.

  **Your code so far**

let array = [];
let largestNum = -1000;
function largestOfFour(arr) {
for(let i=0; i<arr.length; i++){
  for(let j=0; j<arr[i].length; j++){
    if(largestNum<arr[i][j]){
      largestNum = arr[i][j];
    }
  }
  array.push(largestNum);
  largestNum = -1000;
}
return array;
}

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/96.0.4664.110 Safari/537.36

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

Remove the last line and it will pass

First, you are using global variables. This will cause issues with consecutive tests.
Second, your “largestNum” is randomly set to -1000. Which might work in this cases, but if the array was [-2000,-3000,-4000,-5000] it would fail. If you are looking for the largest number of an array, try to think about a startingpoint within the array.

1 Like

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