Create a 2x2, 3x3 or NxN matrix with JS

twitter logo github logo ・1 min read

The matrix 2x2 is simple, :
I think the matrix of 2x2 and 3x3 is easy.

Example: Matrix [2x2];

var det=(matriz[0][0]*matriz[1][1])-(matriz[1][0]*matriz[0][1]);

Example: Matrix [3x3];

var r1=(matriz[0][0]*matriz[1][1]*matriz[2][2])+(matriz[0][1]*matriz[1][2]*matriz[2][0])+(matriz[0][2]*matriz[1][0]*matriz[2][1]);
var r2=(matriz[0][2]*matriz[1][1]*matriz[2][0])+(matriz[0][0]*matriz[1][2]*matriz[2][1])+(matriz[0][1]*matriz[1][0]*matriz[2][2]);
var result = r1-r2;

First create a function for get size the matrix.

 function Matriz(size){
     var matriz = new Array(size);
         for(i = 0; i < size ; i++){
                matriz[i]=new Array(size);

     return matriz;

to be continue

twitter logo DISCUSS
Classic DEV Post from May 22 '19

Coding Best Practices, Chapter One: Functions.

Code best practices, how to write readable functions.

Valentin Antonio profile image
Front-end web developer . I ❤️ tacos.