Basic Algorithm Scripting - Return Largest Numbers in Arrays

Tell us what’s happening:
Can someone explain why this code isn’t working? I’ve gotten it to console log the right values but when I try to run the code it doesn’t accept it as the correct answer

Your code so far

let largestNum = [];
function largestOfFour(arr) {
  for(let i = 0; i < arr.length; i++) {
    largestNum.push(Math.max(...arr[i]));   
  }
  arr = largestNum;
  console.log(largestNum);
  return arr; 
}

largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [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/116.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Return Largest Numbers in Arrays

Link to the challenge:

Works for me. Also, I would consider declaring let largestNum = []; inside the function block instead, since it exists only for that function.

Try resetting the lesson.

1 Like

Ahh that worked! I guess I needed the let inside the function. Thanks!

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