Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function DiagonalAttack(input){
- let matrix=input.map(row => row.split(" ").map(Number));
- let leftDiagonal=0;
- let rightDiagonal=0;
- let count=0;
- let result;
- for (const row of matrix) {
- leftDiagonal+=row[count];
- rightDiagonal+=row[row.length-1-count];
- count++;
- }
- if(leftDiagonal===rightDiagonal){
- let changeCount=0;
- for (const row of matrix) {
- for (let index = 0; index < row.length; index++) {
- if(changeCount!=index && row.length-1-changeCount!=index){
- row[index]=leftDiagonal;
- }
- }
- changeCount++;
- }
- }
- result=matrix.map(row => row.join(" ")).join("\n");
- console.log(result);
- }
Advertisement
Add Comment
Please, Sign In to add comment