title: Convert from C to Javascript: Sudoku solution generator
body: <p>I wrote a program in C that generates a sudoku puzzle solution. It randomly generates numbers, filling in the puzzle as it goes. If it ever tries to fill one spot more than 20 times, it resets everything to 0 and starts over.</p>
  
  <p>The code works wonderfully in C. I can't get it to work in JavaScript though. I'm not trying to do anything fancy with it, just display it on a webpage. When I run the script, it crashes the page (I eventually get a 'page not responding' message).</p>
  
  <p>I'd greatly appreciate any feedback on where I am going wrong here. </p>
  
  <p>The C code is below the JavaScript.</p>
  
  <p>In JavaScript:</p>
  
  <pre class="lang-js prettyprint-override"><code>function Sudoku2() {
      /* constant */
      var LEN = 9;
  
      /* Track numbers in use */
      var blockNums = new Array(LEN);
      var rowNums = new Array(LEN);
      var colNums = new Array(LEN);
      /* solution by blocks (e.g. [0][0-8] is block 0) */
      var solutionBlocks = new Array(LEN);
      /* final puzzle solution (rows x cols) */
      var solution = new Array(LEN);
      /* Track where we are */
      var row, col, block, num = 0, iterations = 0;
  
      /* make arrays 2d */
      for (i=0; i &lt; LEN; i++) {
          blockNums[i] = new Array(LEN);
          rowNums[i] = new Array(LEN);
          colNums[i] = new Array(LEN);
          solutionBlocks[i] = new Array(LEN);
          solution[i] = new Array(LEN);
          solution[i][9] = "&lt;br /&gt;";
      }
  
      /* Generate solution by block */
      for (block = 0; block &lt; LEN; block++) {
          for (i = 0; i &lt; LEN; ) {
              /* Generate a random number */
              num = Math.floor(Math.random()*LEN) + 1;
  
              /* If iteration is &gt; 20 for any i, the solution is unsolvable */
              if (iterations &gt; 20) {
                  /* Reset everything */
                  for (j = 0; j &lt; LEN; j++) {
                      for (k = 0; k &lt; LEN; k++) {
                          solution[j][k] = 0;
                          solutionBlocks[j][k] = 0;
                          blockNums[j][k] = 0;
                          rowNums[j][k] = 0;
                          colNums[j][k] = 0;
                      }
                  }
                  i = 0;
                  iterations = 0;
                  block = 0;
              }
  
              /* is number already assigned to block? */
              if (blockNums[block][num - 1] === 0) {
                  /* convert block number/position to row and col */
                  col = ((block % 3) * 3) + (i % 3);
                  row = (i / 3) + ((block / 3) * 3);
                  /* if number not assigned to row or col */
                  if (rowNums[row][num - 1] === 0 &amp;&amp; colNums[col][num - 1] === 0) {
                      /* assign number */
                      solutionBlocks[block][i] = num;
                      solution[row][col] = num;
                      blockNums[block][num - 1] = 1;
                      rowNums[row][num - 1] = 1;
                      colNums[col][num - 1] = 1;
                      iterations = 0;
                      i++;
                  /* otherwise, track # of loops */
                  } else { iterations++; }
              }
          }
      }
  
  
  
      /* Generate string output */
      var str = solution.toString();
      var screenOutput = document.getElementById("sudoku2");
      screenOutput.innerHTML=str;
  }
  </code></pre>
  
  <p>In C:</p>
  
  <pre class="lang-c prettyprint-override"><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;time.h&gt;
  #define LEN 9
  
  int main ()
  {
      /* Track numbers in use */
      int blockNums[LEN][LEN] = {0};
      int rowNums[LEN][LEN] = {0};
      int colNums[LEN][LEN] = {0};
      /* Track where we are */
      int i = 0, row, col, block, num = 0, iterations = 0;
  
      /* solution by blocks (e.g. [0][0-8] is block 0) */
      int solutionBlocks[LEN][LEN] = {0};
      /* final puzzle solution (rows x cols) */
      int solution[LEN][LEN] = {0};
  
      /* initialize random seed */
      srand ( time(NULL) );
  
      /* Generate solution by block */
      for (block = 0; block &lt; LEN; block++) {
          for (i = 0; i &lt; LEN; ) {
              /* Generate a random number */
              num = (rand() % LEN) + 1;
  
              /* If iteration is &gt; 20 for any i, the solution is unsolvable */
              if (iterations &gt; 20) {
                  int j, k;
                  /* Reset everything */
                  for (j = 0; j &lt; LEN; j++) {
                      for (k = 0; k &lt; LEN; k++) {
                          solution[j][k] = 0;
                          solutionBlocks[j][k] = 0;
                          blockNums[j][k] = 0;
                          rowNums[j][k] = 0;
                          colNums[j][k] = 0;
                      }
                  }
                  i = 0;
                  iterations = 0;
                  block = 0;
              }
  
              /* is number already assigned to block? */
              switch (blockNums[block][num - 1]) {
                  case 0: 
                          /* convert block number/position to row and col */
                          col = ((block % 3) * 3) + (i % 3);
                          row = (i / 3) + ((block / 3) * 3);
                          /* if number not assigned to row or col */
                          if (rowNums[row][num - 1] == 0 &amp;&amp; colNums[col][num - 1] == 0) {
                              /* assign number */
                              solutionBlocks[block][i] = num;
                              solution[row][col] = num;
                              blockNums[block][num - 1] = 1;
                              rowNums[row][num - 1] = 1;
                              colNums[col][num - 1] = 1;
                              iterations = 0;
                              i++;
                          /* otherwise, track # of loops */
                          } else { iterations++; }
                          break;
              }
          }
      }
  
      /* Print solution */
      for(row = 0; row &lt; LEN; row++) {
          printf("   ");
          for(col = 0; col &lt; LEN; col++) {
              printf("%d ", solution[row][col]);
              if (((col + 1) % 3 == 0) &amp;&amp; (col != 8))
                  printf(" ");
          }
          printf("\n");
          if (((row + 1) % 3 == 0) &amp;&amp; (row != 8))
              printf("\n");
      }
  
      return 0;
  }
  </code></pre>
  
  <p>If it is needed, here is the HTML I am using:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;p id="sudoku2"&gt;Click it.&lt;/p&gt;
  &lt;button type="button" onclick="Sudoku2()"&gt;it.&lt;/button&gt;
  </code></pre>