Advertisement
Guest User

Magic Matrices

a guest
Oct 8th, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function magic(input) {
  2.  
  3.     let sum = input[0].reduce((a, b) => a + b, 0);
  4.  
  5.  
  6.     for (let row = 1; row < input.length; row++) {
  7.         let rowSum = input[row].reduce((a, b) => a + b, 0);
  8.  
  9.         if (rowSum !== sum) {
  10.             console.log("false");
  11.             return;
  12.         }
  13.     }
  14.  
  15.     for (let row = 0; row < input.length; row++) {
  16.         let colSum = input.reduce((a, b) => a + b[row], 0);
  17.  
  18.         if (colSum !== sum) {
  19.             console.log("false");
  20.             return;
  21.         }
  22.     }
  23.  
  24.     console.log("true");
  25. }
  26.  
  27. magic([[4, 5, 6],
  28.     [6, 5, 4],
  29.     [5, 5, 5]]);
  30.  
  31. magic([[11, 32, 45],
  32.     [21, 0, 1],
  33.     [21, 1, 1]]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement