This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const nums = [1, 10, 100]; | |
| const matrix = [nums, nums, nums]; // each nums array represents a row in matrix | |
| // reduce accumulates, for each row, the sum of elements in the same column | |
| const columnsSum = matrix.reduce( (accumulated, actual) => { | |
| // this sums element by element, the accumulated array and the actual row | |
| return accumulated.map( (n, index) => { | |
| return n + actual[index]; | |
| }); | |
| }); | |
| console.log(colSums); // prints [3, 30, 300] |