Skip to content

Commit

Permalink
Merge pull request #24 from srmarohit/rohit/add-matrices
Browse files Browse the repository at this point in the history
Rohit/add matrices
  • Loading branch information
ronitsrma16 authored May 11, 2024
2 parents d25449e + 8c16900 commit 6fab478
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions rohit/js/addMatrices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function addMatrices(mat1, mat2) {
if (mat1.flat().length !== mat2.flat().length) {
alert("invalid matrices..");
return;
} else {
const result = [];
let rowLength = mat1.length;

for (let row = 0; row < rowLength; row++) {
if (mat1[row].length !== mat2[row].length) {
alert("invalid Matrices..");
return;
} else {
const resultRow = [];
let colLength = mat1[row].length;
for (let col = 0; col < colLength; col++) {
resultRow.push(Number(mat1[row][col]) + Number(mat2[row][col]));
}
result.push(resultRow);
}
}

return result;
}
}

const mat1 = [
[1, 2],
[3, 1],
];
const mat2 = [
[2, 3],
[4, 2],
];

const result = addMatrices(mat1, mat2);
console.log(result);

0 comments on commit 6fab478

Please sign in to comment.