Advertisement
vaakata

DiagonalsAttack_3.10.16

Oct 3rd, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function diagonalsAttack(input){
  2.     let inputMatrix = input.map(row => row.split(' ').map(Number));
  3.     let sumMainDiag = 0;
  4.     let sumSecDiag = 0;
  5.  
  6.     for(let rowSum = 0; rowSum < input.length; rowSum++){
  7.         for(let colSum = 0; colSum < inputMatrix[rowSum].length; colSum++){
  8.             if(rowSum == colSum){
  9.                 sumMainDiag += inputMatrix[rowSum][colSum];
  10.             }
  11.  
  12.             if(rowSum == (inputMatrix[rowSum].length - 1) - colSum){
  13.                 sumSecDiag += inputMatrix[rowSum][colSum];
  14.             }
  15.         }
  16.     }
  17.  
  18.     if(sumMainDiag == sumSecDiag){
  19.         for (let rowOutput = 0; rowOutput < inputMatrix.length; rowOutput++) {
  20.             for (let colOutput = 0; colOutput < inputMatrix.length; colOutput++) {
  21.                 if(rowOutput != colOutput
  22.                     && rowOutput != (inputMatrix[rowOutput].length - 1) - colOutput){
  23.                     let currentRow = inputMatrix[rowOutput].map(Number);
  24.                     currentRow[colOutput] = 15;
  25.                     inputMatrix[rowOutput] = currentRow;
  26.                 }
  27.             }
  28.         }
  29.         console.log(inputMatrix.map(row => row.join(' ')).join('\n'));
  30.     }
  31.     else{
  32.         console.log(inputMatrix.map(row => row.join(' ')).join('\n'));
  33.     }
  34. }
  35.  
  36. diagonalsAttack(['5 3 12 3 1', '11 4 23 2 5', '101 12 3 21 10', '1 4 5 2 2', '5 22 33 11 1'] )
  37. diagonalsAttack(['1 1 1', '1 1 1', '1 1 0']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement