kalinx

da

Sep 18th, 2021
807
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function diagonalAttack(arr) {
  2.     let diagIndexes = [];
  3.     let sum1 = 0;
  4.     let sum2 = 0;
  5.     let mtrx = [];
  6.     for (let el of arr) {
  7.         mtrx.push(el.split(' ').map((n) => Number(n)));
  8.     }
  9.     for (let i=0; i < mtrx.length; i++) {
  10.         sum1 += mtrx[i][i];
  11.         sum2 += mtrx[mtrx.length - i - 1][i];
  12.         diagIndexes.push(`${i}-${i}`, `${mtrx.length - i - 1}-${i}`);
  13.     }
  14.  
  15.     if (sum1 == sum2) {
  16.         for (let r = 0; r < mtrx.length; r++) {
  17.             for (let c = 0; c < mtrx.length; c++) {
  18.                 if (diagIndexes.includes(`${r}-${c}`)) continue;
  19.                 mtrx[r][c] = sum1;
  20.             }
  21.         }
  22.     }
  23.     mtrx.flatMap((row) => console.log(row.join(' ')))
  24. }
  25.  
RAW Paste Data