quadratic formula

admin

Administrator
Staff member
simple question... why doesnt this work?
it works for the equation 2x^2 - 16x = 30
but not for any thing else...

<script language="JavaScript">
function calculate()
{
var frmQ = document.quadratic;

frmQ.q1.value;
frmQ.q2.value;
frmQ.q3.value;

var varA = Number(frmQ.q1.value);
var varB = Number(frmQ.q2.value);
var varC = Number(frmQ.q3.value);

var sqrB = varB * varB;
var tndPart = 4 * varA * varC;
var last = 2 * varA;
var val = 0;
var val2 = 0;

var sqrtStmtVal = sqrB - tndPart;
var sqrtStmtVal2 = sqrB + (-1 * tndPart);

if (sqrtStmtVal < 0) sqrtStmtVal = sqrtStmtVal * -1;
if (varB < 0) varB = varB * -1;

var sqrtStmt = Math.sqrt(sqrtStmtVal);
val = varB + sqrtStmt;
val = val / last;
frmQ.sol.value = round(val);

if (sqrtStmtVal2 < 0) sqrtStmtVal2 = sqrtStmtVal2 * -1;

var sqrtStmt2 = Math.sqrt(sqrtStmtVal2);
val2 = (-1 * varB) + sqrtStmt2;
if (val2 < 0) val2 = val2 * -1;

val2 = val2 / last;
frmQ.sol2.value = round(val2);
}

function round(x)
{
return Math.round(x*100)/100;
}


</script>
 
Back
Top