Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As Samuel pointed out, you need to call calcRec inside of calcDet (which is called by the onclick event). All you're doing right now is defining a function inside of a function.</p> <p>It looks like you were trying to implement <a href="http://professorjava.weebly.com/matrix-determinant.html" rel="nofollow">this</a> in Javascript (you left a few artifacts, such as the use of the variable "matrix" at the bottom where you previously used "A"). If that was the goal, this should be pretty close to what you were going for:</p> <pre><code>function calcDet() { var A = []; //generates the array for (var i = 0; i &lt; k; i++) { A[i] = []; for (var j = 0; j &lt; k; j++) { A[i][j] = i + ':' + j; } } for (var i = 0; i &lt; k; i++) { //reads the array for (var j = 0; j &lt; k; j++) { var id = "A" + (i + 1) + (j + 1); A[i][j] = parseFloat(document.getElementById(id).value); } } function calcRec(A) { var s; var det = 0; if (A.length == 1) { //bottom case of the recursive function return A[0][0]; } if (A.length == 2) { det = A[0][0] * A[1][1] - A[1][0] * A [0][1]; return det; } for (var i = 0; i &lt; k; i++) { //creates smaller matrix- values not in same row, column var smaller = new Array(A.length - 1); for (h = 0; h &lt; smaller.length; h++) { smaller[h] = new Array(A.length - 1); } for (a = 1; a &lt; A.length; a++) { for (b = 0; b &lt; A.length; b++) { if (b &lt; i) { smaller[a - 1][b] = A[a][b]; } else if (b &gt; i) { smaller[a - 1][b - 1] = A[a][b]; } } } if (i % 2 == 0) { s = 1; } else { s = -1; } det += s * A[0][i] * (calcRec(smaller)); } return (det); } alert(calcRec(A)); } </code></pre> <p>The scope of your "i" loop for creating the "smaller" array was wrong and the initialization of "det" being outside of "calcRec" could cause some undefined behavior.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload