Basic Algorithm Scripting - Return Largest Numbers in Arrays

Tell us what’s happening:
Code works fine, but the output in the challenges should be [27, 5, 39, 1001] and I get [ 5, 27, 39, 1001 ]. Why should the 27 and 5 be switched?

Your code so far

function largestOfFour(arr) {
  let num = [];
  let lnum = 0;
  for(let i = 0; i <= arr.length; i++){
    //console.log(arr[i]);
    for(let x = 0; x <= arr[i].length; x++){
      //console.log(arr[i][x]);
      if(arr[i][x] > lnum){
        lnum = arr[i][x];
      }
    }
    num.push(lnum);
    console.log(num);
  }
  return num;
}

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

Challenge: Basic Algorithm Scripting - Return Largest Numbers in Arrays

Link to the challenge:

The tests disagree with you.

I also get :

TypeError: Cannot read properties of undefined (reading ‘length’)

You may want to review how to loop through an array, how the index works.

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