Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function check(matrix) {
- function getFirstSum(row) {
- let sum = 0;
- for (let i = 0; i < row.length; i++) {
- sum+=row[i];
- }
- return sum;
- }
- let firstSum = getFirstSum(matrix[0]); // get the sum from the 1st matrix row
- let isMagic = true;
- let currSumRow = 0;
- let currSumCol = 0;
- for (let i = 0; i < matrix.length; i++) {
- for (let j = 0; j < matrix[i].length; j++) {
- currSumRow += matrix[i][j];
- currSumCol += matrix[j][i];
- }
- if(currSumRow!== firstSum || currSumCol!==firstSum){ // compare current rol/col sum with the initial sum
- isMagic = false;
- }
- currSumRow=0;
- currSumCol=0
- }
- console.log(isMagic);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement