Quick actions

cmd+k|ctrl+k

Navigation

Languages

Backend02

Snippet info

Language

JavaScript

Visibility

public

Author

64160280

Created

2023-09-28T07:38:48.85055Z

Updated

2023-09-28T07:38:48.85055Z

function exam02(arry) {
  // declare varibles for sum values
  let sumValue = 0;
  let sumValue2 = 0;
  if(arry.length < 3){
  for(let row = 0; row < arry.length; row++) {
    for(let col = 0; col < arry[row].length; col++) {
        if(row === col){
          sumValue += arry[row][col];
        }else if ((row + col) === (arry[row].length-1)){
          sumValue2 += arry[row][col];
        }
      }
    }
  }
  if(arry.length === 3){
    let count = 0; 
    for(let row = 0; row < arry.length; row++) {
      for(let col = 0; col < arry.length; col++) {
          if(row === col){
            sumValue += arry[row][col];
          }else if ((row + col) === (arry[row].length-1)){
            sumValue2 += arry[row][col];
          }
          else if (row === 1 && count < 1){
            count++;
            sumValue2 += arry[1][1];
          }
        }
      }
  }

  if (arry.length > 3) {
    for (let row = 0; row < arry.length; row++) {
      sumValue = arry[row][row];
      sumValue2 = arry[row][(arry.length - row) - 1];
    }
  }
  return Math.abs(sumValue - sumValue2);
}

function test_01() {
  if (
    exam02([
      [1, 2],
      [3, 4],
    ]) == 0
  ) {
    console.log("pass");
  } else {
    console.log("fail");
  }
  if (
    exam02([
      [1, 2, 4],
      [4, 5, 9],
      [7, 8, 9],
    ]) == 1
  ) {
    console.log("pass");
  } else {
    console.log("fail");
  }
}

test_01();
INFO