Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

matrix calculator in js

let a = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
];
let b = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
];

let result = 0;

if (a.length === 3 && b.length === 3) {
  console.log(a[0][0] + b[0][0], a[0][1] + b[0][1], a[0][2] + b[0][2]);
  console.log(a[1][0] + b[1][0], a[1][1] + b[1][1], a[1][2] + b[1][2]);
  console.log(a[2][0] + b[2][0], a[2][1] + b[2][1], a[2][2] + b[2][2]);
  
  // this nested for loop shows the output in one line rather than in matrix form
  /*
  for (let row = 0; row <= 2; row++) {
    for (let col = 0; col <= 2; col++) {
      result = a[row][col] + b[row][col];
      console.log(result);
     }
  }
  */
} else {
  console.log("It works only for 3x3 matrix");
}
 
PREVIOUS NEXT
Tagged: #matrix #calculator #js
ADD COMMENT
Topic
Name
2+3 =