title: Does this method work for solving the quadratic equation using JavaScript?
body: <p>I'm trying to do some "complex" math where I need to call upon some of JavaScript's Math properties to solve the quadratic equation.  Does the following method work?</p>
  
  <pre><code>    root = Math.pow(inputb,2) - 4 * inputa * inputc;
      root1 = (-inputb + Math.sqrt(root))/2*inputa;
      root2 = (-inputb - Math.sqrt(root))/2*inputa;
  </code></pre>
  
  <p>Does this look correct?</p>
  
  <p>For some reason, I'm not seeing correct results..</p>
  
  <p><code>inputa</code>, <code>inputb</code>, and <code>inputc</code> are all variables which store user-input from a text field by the way.</p>
  
  <p><strong>FULL CODE</strong></p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
  &lt;title&gt;Quadratic Root Finder&lt;/title&gt;
  
  &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
  
  &lt;script&gt;
  window.onload = function() {
  
  
  
      $('.error').hide();
  
  
  
      document.getElementById('quadraticcalculate').onclick = function calculateQuad()
      {
          var inputa = document.getElementById('variablea').value;
          var inputb = document.getElementById('variableb').value;
          var inputc = document.getElementById('variablec').value;
  
          inputa = new Number(inputa); // try to convert to number
          if (isNaN(inputa)) { // use built in method to check for NaN
              $('#quadraticaerror').show();
              return;
          }
  
          inputb = new Number(inputb); // try to convert to number
          if (isNaN(inputb)) { // use built in method to check for NaN
              $('#quadraticberror').show();
              return;
          }
  
          inputc = new Number(inputc); // try to convert to number
          if (isNaN(inputc)) { // use built in method to check for NaN
              $('#quadraticcerror').show();
              return;
          }
  
          root = Math.pow(inputb,2) - 4 * inputa * inputc;
          root1 = (-inputb + Math.sqrt(root))/(2*inputa);
          root2 = (-inputb - Math.sqrt(root))/(2*inputa);
  
          document.getElementById('root1').value = root1;
          document.getElementById('root2').value = root2;
          if(root&lt;'0')
          {
              document.getElementById('root1').value = 'No real solution'
              document.getElementById('root2').value = 'No real solution'
          }
          else {
              if(root=='0')
              {
                  document.getElementById('root1').value = root1
                  document.getElementById('root2').value = 'No Second Answer'
              }
              else {
                  document.getElementById('root1').value = root1
                  document.getElementById('root2').value = root1
                  }
              }
      };
  
  
  
      document.getElementById('quadraticerase').onclick = function()
      {
          document.getElementById('quadraticform').reset();
          $('.error').hide();
      }
  
          document.getElementById('cubicerase').onclick = function()
      {
          document.getElementById('cubicform').reset();
          $('.error').hide();
      }
  
  
  
  }
  &lt;/script&gt;
  
  &lt;style&gt;
  div.#wrapper
  {
      text-align: center;
  }
  .error
  {
      color: #FF0000;
  }&lt;/style&gt;
  
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;div id="wrapper"&gt;
      &lt;div id="quadratic"&gt;
          &lt;form id="quadraticform"&gt;
              &lt;h1&gt;Quadratic&lt;/h1&gt;
              a:&lt;input id="variablea" value="" type="text"&gt;
              &lt;br/&gt;
              b:&lt;input id="variableb" value="" type="text"&gt;
              &lt;br /&gt;
              c:&lt;input id="variablec" value="" type="text"&gt;
              &lt;br /&gt;
              &lt;input id="quadraticcalculate" value="Calculate!" type="button"&gt;
              &lt;input id="quadraticerase" value="Clear" type="button"&gt;
              &lt;br /&gt;
              &lt;br /&gt;
              Roots:
              &lt;br /&gt;
              &lt;input id="root1" type="text" readonly&gt;
              &lt;br /&gt;
              &lt;input id="root2" type="text" readonly&gt;
              &lt;p id="quadraticaerror" class="error"&gt;Error:  Variable a is not a valid integer!&lt;/p&gt;
              &lt;br /&gt;
              &lt;p id="quadraticberror" class="error"&gt;Error:  Variable b is not a valid integer!&lt;/p&gt;
              &lt;br /&gt;
              &lt;p id="quadraticcerror" class="error"&gt;Error:  Variable c is not a valid integer!&lt;/p&gt;
          &lt;/form&gt;
      &lt;/div&gt;
      &lt;div id="cubic"&gt;
          &lt;form id="cubicform"&gt;
              &lt;h1&gt;Cubic&lt;/h1&gt;
              a:&lt;input id="variablea" value="" type="text"&gt;
              &lt;br/&gt;
              b:&lt;input id="variableb" value="" type="text"&gt;
              &lt;br /&gt;
              c:&lt;input id="variablec" value="" type="text"&gt;
              &lt;br /&gt;
              d:&lt;input id="variabled" value="" type="text"&gt;
              &lt;br /&gt;
              &lt;input id="cubiccalculate" value="Calculate!" type="button"&gt;
              &lt;input id="cubicerase" value="Clear" type="button"&gt;
              &lt;br /&gt;
              &lt;br /&gt;
              Roots:
              &lt;br /&gt;
              &lt;input id="root1" type="text" readonly&gt;
              &lt;br /&gt;
              &lt;input id="root2" type="text" readonly&gt;
              &lt;p id="cubicaerror" class="error"&gt;Error:  Variable a is not a valid integer!&lt;/p&gt;
              &lt;br /&gt;
              &lt;p id="cubicberror" class="error"&gt;Error:  Variable b is not a valid integer!&lt;/p&gt;
              &lt;br /&gt;
              &lt;p id="cubiccerror" class="error"&gt;Error:  Variable c is not a valid integer!&lt;/p&gt;
              &lt;br /&gt;
              &lt;p id="cubicderror" class="error"&gt;Error:  Variable d is not a valid integer!&lt;/p&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>