Aliendreamer

magic matrices

Jan 31st, 2019
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function magicMatrices(input) {
  2.     let matrix = input;
  3.     let sum = matrix[0].reduce((a,b) => a+b);
  4.     let isMagic = true;
  5.  
  6.     for(let i=1; i<matrix.length; i++) {
  7.         if(sum != matrix[i].reduce((a,b) => a+b)) {
  8.             isMagic = false;
  9.         }
  10.     }
  11.  
  12.     for(let col=0; col<matrix[0].length; col++) {
  13.         let sumCol = 0;
  14.         for(let row=0; row<matrix.length; row++) {
  15.             sumCol += matrix[row][col];
  16.         }
  17.  
  18.         if(sumCol != sum) {
  19.             isMagic = false;
  20.         }
  21.     }
  22.  
  23.     console.log(isMagic);
Advertisement
Add Comment
Please, Sign In to add comment