Advertisement
minchevaa

diagonalAttack

Feb 1st, 2023
670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 0.88 KB | Source Code | 0 0
  1. function diagonalAttack(matrix) {
  2.  
  3.     let firstDiagonal = 0;
  4.     let secondDiagonal = 0;
  5.  
  6.     for (let i = 0; i < matrix.length; i++) {
  7.         firstDiagonal += +matrix[i].split(" ")[i];
  8.         secondDiagonal += +matrix[i].split(" ")[matrix.length - 1 - i];
  9.     }
  10.  
  11.     if (firstDiagonal === secondDiagonal) {
  12.  
  13.         for (let i = 0; i < matrix.length; i++) {
  14.  
  15.             let currentString = matrix[i].split(" ");
  16.  
  17.             for (let j = 0; j < currentString.length; j++) {
  18.  
  19.                 if (j !== i && j !== matrix.length - 1 - i) {
  20.                     currentString[j] = firstDiagonal;
  21.                 }
  22.             }
  23.             matrix[i] = currentString.join(" ");
  24.         }
  25.         console.log(matrix.join("\n"));
  26.     }
  27. }
  28.  
  29. // diagonalAttack(['5 3 12 3 1',
  30. //     '11 4 23 2 5',
  31. //     '101 12 3 21 10',
  32. //     '1 4 5 2 2',
  33. //     '5 22 33 11 1']
  34. // );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement