Advertisement
Ivankooo1

Magic Matrix

Sep 15th, 2020
1,119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function magicMatrices(matrix) {
  2.  
  3.     for(let i = 0;i < matrix.length-1;i++){
  4.         let rowOne = matrix[0].reduce((p,c,i,a)=> p + c,0);
  5.         let rowTwo = matrix[1].reduce((p,c,i,a)=> p + c,0);
  6.         let colOne = 0;
  7.         let colTwo = 0;
  8.        
  9.         for(let a = 0;a < matrix.length;a++){
  10.             colOne += matrix[i][a];
  11.             colTwo += matrix[i + 1][a];
  12.         }
  13.  
  14.         if(rowTwo != rowOne || colTwo != colOne){
  15.             return false;
  16.         }
  17.     }
  18.     return true;
  19. }
  20.  
  21. console.log(magicMatrices(
  22.    [[4, 5, 6],
  23.     [6, 5, 4],
  24.     [5, 5, 5]]
  25.    ))
  26.  
  27.    console.log(magicMatrices(
  28.     [[11, 32, 45],
  29.     [21, 0, 1],
  30.     [21, 1, 1]]
  31.  
  32.     ))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement