Advertisement
elena1234

Iterate Through Rows And Cols - JavaScript

Aug 19th, 2021
2,145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(matrix) {
  2.     let arrayForRows = [];
  3.     let sumRow = 0;
  4.     for (let row = 0; row < matrix.length; row++) {
  5.         for (let col = 0; col < matrix[row].length; col++) {
  6.             sumRow += matrix[row][col];
  7.         }
  8.  
  9.         arrayForRows.push(sumRow);
  10.         sumRow = 0;
  11.     }
  12.  
  13.     let arrayForCols = [];
  14.     let sumCol = 0;
  15.     for (let col = 0; col < matrix[0].length; col++) {
  16.         for (let row = 0; row < matrix.length; row++) {
  17.             sumCol += matrix[row][col];
  18.         }
  19.  
  20.         arrayForCols.push(sumCol);
  21.         sumCol = 0;
  22.     }
  23.  
  24.     let concatArray = arrayForRows.concat(arrayForCols);
  25.     console.log(concatArray.every(x => x === concatArray[0]));
  26. }
  27.  
  28. solve ([[4, 5, 6],
  29.     [6, 5, 4],
  30.     [5, 5, 5]]
  31.    )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement