title: Multiply 2 matrices in Javascript
body: <p>I'm doing a function that multiplies 2 matrices.</p>
  <p>The matrices will always have the same number of rows and columns. (2x2, 5x5, 23x23, ...)</p>
  <p>When I print it, it doesn't work. Why?</p>
  <p>For example, if I create two 2x2 matrices:</p>
  <p>matrixA:</p>
  <pre><code>[1][2]
  
  [3][4]
  </code></pre>
  <p>matrixB:</p>
  <pre><code>[5][6]
  
  [7][8]
  </code></pre>
  <p>The result should be:</p>
  <pre><code>[19][22]
  
  [43][50]
  </code></pre>
  <p>(<a href="http://ncalculators.com/matrix/2x2-matrix-multiplication-calculator.htm" rel="nofollow noreferrer">http://ncalculators.com/matrix/2x2-matrix-multiplication-calculator.htm</a>)</p>
  <p>But, I get:</p>
  <pre><code>[19][undefined]
  
  [22][indefined]
  </code></pre>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function multiplyMatrix(matrixA, matrixB) {
    var result = new Array(); //declare an array   
  
    //var numColsRows=$("#matrixRC").val();
    numColsRows = 2;
  
    //iterating through first matrix rows
    for (var i = 0; i &lt; numColsRows; i++) {
      //iterating through second matrix columns
      for (var j = 0; j &lt; numColsRows; j++) {
        var matrixRow = new Array(); //declare an array
        var rrr = new Array();
        var resu = new Array();
        //calculating sum of pairwise products
        for (var k = 0; k &lt; numColsRows; k++) {
          rrr.push(parseInt(matrixA[i][k]) * parseInt(matrixB[k][j]));
        } //for 3
        resu.push(parseInt(rrr[i]) + parseInt(rrr[i + 1]));
  
        result.push(resu);
        //result.push(matrixRow);
      } //for 2
    } //for 1
    return result;
  } // function multiplyMatrix</code></pre>
  </div>
  </div>
  </p>