Advertisement
kstoyanov

07. Magic Matrices

Sep 18th, 2020
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(matrix) {
  2.   for (let i = 0; i < matrix.length - 1; i++) {
  3.     const sumRowOne = matrix[i].reduce((a, b) => a + b, 0);
  4.     const sumRowTwo = matrix[i + 1].reduce((a, b) => a + b, 0);
  5.     let sumCollOne = 0;
  6.     let sumCollTwo = 0;
  7.  
  8.     for (let j = 0; j < matrix.length; j++) {
  9.       sumCollOne += matrix[i][j];
  10.       sumCollTwo += matrix[i + 1][j];
  11.     }
  12.  
  13.     if (sumRowOne !== sumRowTwo || sumCollOne !== sumCollTwo) {
  14.       return false;
  15.     }
  16.   }
  17.  
  18.   return true;
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement