| Viewed 147 times

How to Sum of all Evens in a Matrix in javaScript?


I need a function that returns the sum of all even elements in a 2D matrix. Examples sumOfEvens([ [1, 0, 2], [5, 5, 7], [9, 4, 3] ]) // 6

1 Answer
Mohammad Qandeel
Answered
23

The two-dimensional array can be defined as an array of arrays

you can try this function its returns exactly what is required

function sumOfEvens(arr) {
return arr.map(arr => arr.
         filter(n => n%2===0).
         reduce((a,n)=> a+n,0)).
         reduce((a,n) => a+n,0)
}


Related Questions