viligen

magicMatrices

May 21st, 2022 (edited)
348
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function myFunc(matrix) {
  2.     let sumsRows = new Set()
  3.     let sumsCols = new Set()
  4.  
  5.     for (let row of matrix) {
  6.         sumsRows.add(row.reduce((a, b) => a + b))
  7.     }
  8.     if (sumsRows.size > 1) { return false }
  9.  
  10.     for (let col = 0; col < matrix.length; col++) {
  11.         let currentSum = 0
  12.         for (let row = 0; row < matrix.length; row++) {
  13.             currentSum += matrix[row][col]
  14.         }
  15.         sumsCols.add(currentSum)
  16.     }
  17.     if (sumsCols.size > 1) { return false }
  18.  
  19.     return Array.from(sumsRows)[0] == Array.from(sumsCols)[0]
  20.  
  21. }
  22.  
  23.  
  24.  
  25.  
  26.  
  27. console.log(myFunc(
  28.     [[4, 5, 6],
  29.     [6, 5, 4],
  30.     [5, 5, 5]]
  31. ))
RAW Paste Data Copied