georgiev955

magicMatrix

May 26th, 2023
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function magixMatrix(matrix) {
  2.     let rowSum = 0;
  3.     let isMagic = true;
  4.     for (let array = 0; array < matrix.length; array++) {
  5.         if (array === 0) {
  6.             for (let index = 0; index < matrix[array].length; index++) {
  7.                 rowSum += Number(matrix[array][index]);
  8.             }
  9.         } else {
  10.             let nextSum = 0;
  11.             for (let index = 0; index < matrix[array].length; index++) {
  12.                 nextSum += Number(matrix[array][index]);
  13.             }
  14.             if (nextSum !== rowSum) {
  15.                 isMagic = false;
  16.                 break;
  17.             }
  18.         }
  19.     }
  20.  
  21.     let colSum = 0;
  22.     for (let x = 0; x < matrix.length; x++) {
  23.         if (x === 0) {
  24.             for (let y = 0; y < matrix.length; y++) {
  25.                 colSum += matrix[y][x];
  26.             }
  27.         } else {
  28.             let nextSum = 0;
  29.             for (let y = 0; y < matrix.length; y++) {
  30.                 nextSum += matrix[y][x];
  31.             }
  32.             if (nextSum !== colSum) {
  33.                 isMagic = false;
  34.                 break;
  35.             }
  36.         }
  37.     }
  38.     console.log(isMagic);
  39. }
Advertisement
Add Comment
Please, Sign In to add comment