title: Mccluskey algorithm, javascript
body: <pre><code>&lt;html&gt;
  &lt;body&gt;
  
  &lt;script type="text/javascript"&gt;
  start();
  
  function start() {
  
      var val = "0,1";
  
      var n = 5;
  
      var chars = ['a', 'b', 'c', 'd', 'e'];
  
      gVars = chars.slice(0, n);
  
      for (var i = 0; i &lt; gVars.length; i++)
  
      document.write(gVars[i] + "&lt;br /&gt;");
  
      var termsStr = val.split(',');
  
      for (var i = 0; i &lt; termsStr.length; i++)
  
      document.write(termsStr[i] + "&lt;br /&gt;");
  
      var gOrigTerms = [];
  
      var maxterm = Math.pow(2, termsStr.length) - 1;
  
      document.write("maxterm: " + maxterm + "&lt;br /&gt;");
  
      for (var i = 0; i &lt; termsStr.length; i++) {
          gOrigTerms[i] = parseInt(termsStr[i]);
          document.write(gOrigTerms[i] + "&lt;br /&gt;");
          if (gOrigTerms[i] &gt; maxterm) document.write("Invalid term in term list." + "&lt;br /&gt;");
      }
  
      gFormula = new Formula(gVars, gOrigTerms);
  
      document.write(gFormula);
  
      gFormula.toString();
  
      gFormula.reduceToPrimeImplicants(); //here the breakpoint is inserted
  }
  
  
  function Formula(vars, terms)
  
  {
  
      this.vars = vars;
  
      this.termList = [];
  
      for (var i = 0; i &lt; terms.length; i++) {
          this.termList[i] = new Term(Dec2Bin(terms[i], vars.length));
          document.write("this.termList" + this.termList[i] + "&lt;br /&gt;");
      }
      this.orginalTermList = [];
      document.write("this.orginalTermList" + this.orginalTermList + "&lt;br /&gt;");
  }
  
  function Dec2Bin(dec, size) {
      var bits = [];
  
      for (var bit = 0; bit &lt; size; bit++)
  
      {
          bits[bit] = 0;
      }
  
      var i = 0;
  
      while (dec &gt; 0)
  
      {
          if (dec % 2 == 0)
  
          {
              bits[i] = 0;
          } else
  
          {
              bits[i] = 1;
          }
  
  
          i++;
  
          dec = (dec / 2) | 0;
  
          // Or with zero casts result to int (who knows why...) 
      }
  
      bits.reverse();
      return bits;
  
  }
  
  
  function Term(varVals)
  
  {
      this.varVals = varVals;
      document.write("this.varVals: " + this.varVals);
  
  }
  
  function reduceToPrimeImplicants() //there is some problem with this function
  {
  
      this.originalTermList = this.termList.slice(0);
  
      var numVars = this.termList[0].getNumVars();
  
      var table = [];
  
      for (var dontKnows = 0; dontKnows &lt;= numVars; dontKnows++) {
          table[dontKnows] = [];
          for (var ones = 0; ones &lt;= numVars; ones++) {
              table[dontKnows][ones] = [];
          }
          table[dontKnows][numVars + 1] = [];
      }
  
  
      table[numVars + 1] = [];
  
      table[numVars + 1][numVars + 1] = [];
  
      for (var i = 0; i &lt; this.termList.length; i++) {
          var dontCares = this.termList[i].countValues(DontCare);
          var ones = this.termList[i].countValues(1);
          var len = table[dontCares][ones].length;
          table[dontCares][ones][len] = this.termList[i];
      }
  
  
  
      for (var dontKnows = 0; dontKnows &lt;= numVars - 1; dontKnows++) {
          for (var ones = 0; ones &lt;= numVars - 1; ones++) {
              var left = table[dontKnows][ones];
              var right = table[dontKnows][ones + 1];
              var out = table[dontKnows + 1][ones];
  
              for (var leftIdx = 0; leftIdx &lt; left.length; leftIdx++) {
                  for (var rightIdx = 0; rightIdx &lt; right.length; rightIdx++) {
                      var combined = left[leftIdx].combine(right[rightIdx]);
  
                      if (combined != null) {
                          if (out.indexOf(combined) &lt; 0) {
                              var len = out.length;
                              out[len] = combined;
                          }
                          if (this.termList.indexOf(left[leftIdx]) &gt;= 0) {
                              this.termList.splice(this.termList.indexOf(left[leftIdx]), 1);
                          }
                          if (this.termList.indexOf(right[rightIdx]) &gt;= 0) {
                              this.termList.splice(this.termList.indexOf(right[rightIdx]), 1);
                          }
                          if (this.termList.indexOf(combined) &lt; 0) {
  
                              var len = this.termList.length;
                              this.termList[len] = combined;
                          }
                      }
                  }
              }
          }
      }
  }
  
  function getNumVars()
  
  {
      return this.varVals.length;
  }
  
  
  function countValues(value)
  
  {
      result = 0;
      for (var i = 0; i &lt; this.varVals.length; i++) {
          if (this.varVals[i] == value) {
              result++;
          }
      }
  
      return result;
  }
  
  
  function combine(term)
  
  {
      var diffVarNum = -1; // The position where they differ 
      for (var i = 0; i &lt; this.varVals.length; i++) {
          {
              if (this.varVals[i] != term.varVals[i])
  
              if (diffVarNum == -1) {
                  diffVarNum = i;
              } else { // They're different in at least two places return null; } 
              }
          }
  
          if (diffVarNum == -1)
  
          {
              // They're identical return null; 
          }
  
          resultVars = this.varVals.slice(0);
  
          resultVars[diffVarNum] = DontCare;
  
          return new Term(resultVars);
  
      }
  &lt;/script&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>In the above code, that is not complete, but which implements quine Mccluskey algorithm. There is a problem while it is debugged.</p>
  
  <p>If a breakpoint is inserted at <code>gFormula.reducetoPrimeImplicants();</code> the debugger does not go into that function. This is the last function called in the code until now. But, it does go to <code>start()</code>, which is the first function. </p>
  
  <p>There is some problem in <code>reducetoPrimeImplicants();</code> function because it also gives ERROR in internet explorer.</p>
  
  <p>I am not able to figure out the error. If I remove <code>reducetoPrimeImplicants();</code> function from the code the works fine.</p>
  
  <p>Please, can somebody tell me why the debugger does not enter <code>reducetoPrimeImplicants();</code>. </p>
  
  <p>I am using the Firebug debugger.</p>
  
  <p>Thanks in advance. </p>