Advertisement
Guest User

07. Magic Matrices

a guest
Jun 4th, 2017
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function check(matrix) {
  2.  
  3.         function getFirstSum(row) {
  4.             let sum = 0;
  5.             for (let i = 0; i < row.length; i++) {
  6.                 sum+=row[i];
  7.             }
  8.             return sum;
  9.         }
  10.  
  11.         let firstSum = getFirstSum(matrix[0]); // get the sum from the 1st matrix row
  12.         let isMagic = true;
  13.         let currSumRow = 0;
  14.         let currSumCol = 0;
  15.  
  16.         for (let i = 0; i < matrix.length; i++) {
  17.             for (let j = 0; j < matrix[i].length; j++) {
  18.                 currSumRow += matrix[i][j];
  19.                 currSumCol += matrix[j][i];
  20.             }
  21.             if(currSumRow!== firstSum || currSumCol!==firstSum){ // compare current rol/col sum with the initial sum
  22.                 isMagic = false;
  23.             }
  24.             currSumRow=0;
  25.             currSumCol=0
  26.         }
  27.  
  28.         console.log(isMagic);
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement