New to js algorithms, need help

Tell us what’s happening:

  **Your code so far**

function largestOfFour(arr) {
let a = [];
let b = 0;
arr.forEach((sub)=>{
  sub.forEach((s)=>{
      if(s>b){
        b = s;
      }
  })
  a.push(b);
  b = 0;
})
return a;
}
console.log(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]))






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

when i run the code it works fine with numbers that are bigger than 0, but in the last sub array it doesn’t work

yes, because none of the numbers are positive, so the greatest number is 0
you need to refactor your code to consider negative numbers

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