compute determinant of a matrix

I'm trying to compute determinant of a matrix in JS. I used algorithm from but I lost my mind on the last condition. I just do not understand. Can you help me?

This is how looks like my code right now. In another function I create an empty 2d array and then copy it to det function. Next I retrieve values from html and then trying to compute determinant of a matrix. The first 2 cases are simple but I have a problem with the last one. I couldn't find working example in JS.

function det() {
  var det = 0;
  var array1 = array.slice();

  for (i = 0; i < array1.length; i++) {

    for (j = 0; j < array1[i].length; j++) {
      array1[i][j] = parseInt(document.getElementById("element" + (i + 1) + (j + 1)).value, 10);


  if (array1.length == 1) {
    det = array1[0][0];
  } else if (array1.length == 2) {
    det = (array1[0][0] * array1[1][1]) - (array1[1][0] * array1[0][1]);
  } else {




  • You can see definition of the determinant for square matrix here Algorithm used in use some properties of determinat to calculate it in recursive way as sum over all permutations. In this way you get N * N! operations! It is very big even for small N.

    For solving this problem you can first transform matrix to triangular with the same determinant and after that calculate determinant as product of all diagonal elements.