Basic Algorithm

I’ve been working on this algorithm for almost a day and cant get it to work. It’s the ‘return largest number in arrays’. Please help.


function largestOfFour(arr) {
  // You can do this!
  let arrAnswer = [];
  let largestNum = 0;
  for(let i = 0; i <= arr.length; i++){
      arrAnswer = arr[i];
    for(let j = 0; j <= arr[i].length; j++){
      if(arr[i][j] > largestNum){
      largestNum = arr[i][j]; 
      }
    }
    }
  
  return largestNum;
}

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

Yes. errors are gone. I’m only getting the largest of the last sub-array though.

Got it. Thx for the help.

Thought I had it done. The algorithm works fine on the browser console but doesn’t pass fcc tests. If someone can tell me why, that would be great.


function largestOfFour(arr) {
  // You can do this!
  let arrAnswer = [];
  for(let i = 0; i < arr.length; i++){
      let largestNum = 0;
    for(let j = 0; j < arr[i].length; j++){
      if(arr[i][j] > largestNum){
      largestNum = arr[i][j]; 
      }
    }
    arrAnswer[i] = largestNum;
    }
   
  return arrAnswer;
}

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

newer mind. fixed it