Advertisement
desito07

Magic Matrices

May 27th, 2021
932
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(array) {
  2.   let sumOfRows = 0;
  3.   array.forEach((row, index) => {
  4.     let isEqual = true;
  5.     if (array[index + 1]) {
  6.       if (
  7.         row.reduce((acc, el) => acc + el) ==
  8.         array[index + 1].reduce((acc, el) => acc + el)
  9.       ) {
  10.         sumOfRows = row.reduce((acc, el) => acc + el);
  11.       } else {
  12.         isEqual = false;
  13.       }
  14.     }
  15.   });
  16.   let counter = 0;
  17.   let sumOfColumns = 0;
  18.   let currSum = 0;
  19.   let fistCHeck = false;
  20.   for (let i = 0; i < array.length; i++) {
  21.     for (let j = 0; j < array.length; j++) {
  22.       sumOfColumns += array[j][counter];
  23.     }
  24.     if (currSum !== sumOfColumns && !fistCHeck) {
  25.       currSum = sumOfColumns;
  26.       fistCHeck = true;
  27.     } else {
  28.       fistCHeck = false;
  29.       break;
  30.     }
  31.     sumOfColumns = 0;
  32.     counter++;
  33.   }
  34.  
  35.   if (sumOfColumns === sumOfRows) {
  36.     console.log(true);
  37.   } else {
  38.     console.log(false);
  39.   }
  40. }
  41. solve([
  42.   [4, 5, 6],
  43.   [6, 5, 4],
  44.   [5, 5, 5],
  45. ]);
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement