Error in looping arrays

Error in looping arrays
0

Tell us what’s happening:
please check the code ,why is the code not working for all cases .
Your code so far


function largestOfFour(arr) {
let boo = -100; //or 0
let newArr=[];
for(let i=0;i<arr.length;i++){
  for(let j=0;j<arr.length;j++){
     if(arr[i][j] > boo){
       boo = arr[i][j];
     }
}newArr.push(boo);
  
}
// console.log(newArr)
 return newArr;
} 

console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]));
//logs [ 5, 27, 39, 1001 ]
console.log(largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]))
//logs [ 27, 27, 39, 1001 ]
console.log(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]))
//logs [ 25, 48, 48, 48 ]




Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36.

Challenge: Return Largest Numbers in Arrays

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arraysPreformatted text

We want some space here and we are not alone
j=0
Greetings from your code

The inner loop is an exact repeat of the outer loop. Try j < arr[i].length

Also, check the timing of your variables with the for loops. The boo variable isn’t in quite the right spot for it to look for the largest number inside each sub array. Where it is now, it is looking for the largest number in all the array.

Hope that helps!

1 Like

thank you ! i’ll have a look