Jump to content

What's Wrong With This Code?


Recommended Posts

Hi. I am working on a quadratic formula solver, just for fun. Here is the code I need fixed:

var $a, $b, $c, $d, $e, $f, $g, $h, $$A, $$B, a, b, c;
function solve(x, y, z) {
 a = document.getElementById("A_").value;
 b = document.getElementById("B_").value;
 c = document.getElementById("C_").value;
$a = (-1 * B)/>;
$b = (2 * a);
$c = Math.pow(b, 2);
$d = (4 * a * c);
$e = ($c - $d);
if ($e >= 0) {
 $f = Math.sqrt($e);
 $g = ($a + $f);
 $h = ($a - $f);
 $$A = ($g / $B)/>;
 $$B = ($h / $B)/>;
if ($$A == $$B)/> {
	return $$A;
} else if (Math.abs($$B)/> == $$A) {
	return "± " + $$A;
} else {
return $$A + " or " + $$B;
} else {
return "I'm sorry, this calculator can not compute imaginary solutions.";
}

Apparently, I am misusing the brackets, parentheses, etc. I have a version that works:

function solve(x, y, z) {
 a = document.getElementById("A_").value;
 b = document.getElementById("B_").value;
 c = document.getElementById("C_").value;
$a = (-1 * B)/>;
$b = (2 * a);
$c = Math.pow(b, 2);
$d = (4 * a * c);
$e = ($c - $d);
if ($e >= 0) {
 $f = Math.sqrt($e);
 $g = ($a + $f);
 $h = ($a - $f);
 $$A = ($g / $B)/>;
 $$B = ($h / $B)/>;
if ($$A == $$B)/> {						  
	return $$A;
} else {
	return $$A + " or " + $$B;			
}
} else {
return "I'm sorry, this calculator can not compute imaginary solutions.";
}
}

But I want to insert the "else if" statement, which I seem to be doing wrong.

Anyone know the (probably embarassingly obvious) solution? If so, can you please show me what to do?

Thanks!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...