-6

im trying to print the max number out of all the nested arrays but so far i only managed to get the max number out of each array individually

would love to get some help and insigth as to what i was doing wrong heres my code so far:

const arr = [ 
    [1,2,3,10,9],
    [4,5,6,8,7],
    [7,8,9,6,5]
]

function matMax(){
    for (const items of arr){
        let max = 0;
        for (const item of items){
            if(items>max){
                max = items
            }
        }
        console.log(max)
    }
}

matMax()```


i dont want to use math.max method 
cjs
  • 21
  • 6
  • 1
    You're resetting your max value before iterating over every sub-array. And you're not actually iterating over the sub-arrays, you're iterating over the main array twice. And you're redefining a `const`. – Robby Cornelissen Apr 10 '23 at 02:30
  • The declaration ```let max = 0``` needs to be outside the loop (it's reset on every element). – Nemo9703 Apr 10 '23 at 02:31

1 Answers1

-1
const arr = [
  [1, 2, 3, 10, 9],
  [4, 5, 6, 8, 7],
  [7, 8, 9, 6, 5]
]

function matMax() {
  for (const items of arr) {
    let max = 0;
    for (const item of items) {
      if (item > max) {
        max = item
      }
    }
    console.log(max)
  }
}

matMax()

Modify the internal loop. The internal loop should loop through the external items, and const defines a new variable item to compare max

enter image description here

If you want to obtain the maximum value in all arrays, you can define the outermost layer of the max variable

like this:

enter image description here

Jeffrey
  • 37
  • 6