Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function diagonalsAttack(input){
- let inputMatrix = input.map(row => row.split(' ').map(Number));
- let sumMainDiag = 0;
- let sumSecDiag = 0;
- for(let rowSum = 0; rowSum < input.length; rowSum++){
- for(let colSum = 0; colSum < inputMatrix[rowSum].length; colSum++){
- if(rowSum == colSum){
- sumMainDiag += inputMatrix[rowSum][colSum];
- }
- if(rowSum == (inputMatrix[rowSum].length - 1) - colSum){
- sumSecDiag += inputMatrix[rowSum][colSum];
- }
- }
- }
- if(sumMainDiag == sumSecDiag){
- for (let rowOutput = 0; rowOutput < inputMatrix.length; rowOutput++) {
- for (let colOutput = 0; colOutput < inputMatrix.length; colOutput++) {
- if(rowOutput != colOutput
- && rowOutput != (inputMatrix[rowOutput].length - 1) - colOutput){
- let currentRow = inputMatrix[rowOutput].map(Number);
- currentRow[colOutput] = 15;
- inputMatrix[rowOutput] = currentRow;
- }
- }
- }
- console.log(inputMatrix.map(row => row.join(' ')).join('\n'));
- }
- else{
- console.log(inputMatrix.map(row => row.join(' ')).join('\n'));
- }
- }
- 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'] )
- diagonalsAttack(['1 1 1', '1 1 1', '1 1 0']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement