Advertisement
Pijomir

Magic Matrices

Oct 2nd, 2022
1,087
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function magicMatrices(arr) {
  2.     let result = true;
  3.     let rowCheck = arr[0].reduce((a, b) => a + b, 0);
  4.  
  5.     for (let i = 0; i < arr.length; i++) {
  6.         let checkSum = arr[i].reduce((a, b) => a + b, 0);
  7.         if (checkSum !== rowCheck) {
  8.             result= false;
  9.             break;
  10.         }
  11.         let checkRow = 0;
  12.         for (let j = 0; j < arr.length; j++) {
  13.             checkRow += arr[i][j];
  14.         }
  15.         if (checkRow !== rowCheck) {
  16.             result = false;
  17.             break;
  18.         }
  19.         checkRow = 0;
  20.     }
  21.     console.log(result ? 'true' : 'false')
  22. }
  23.  
  24. magicMatrices([[4, 5, 6],
  25.     [6, 5, 4],
  26.     [5, 5, 5]]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement