/    Sign up×
Community /Pin to ProfileBookmark

Script Problem: Quadratic Formula

This script was written to solve the quadratic equation and return the two x intercepts. For some reason, the two values are sometimes opposite (positive, negative etc.). Not all the time, but most of it. For example, if you plug in 1x(squared)+10x+21, it will return the values -3 and -7, when the answer is really 3 and 7. Please help!

function Calculate()
{
var a = document.Input.a.value
var b = document.Input.b.value
var c = document.Input.c.value

// _____________
// -b +/- /b^2 – 4ac
// x=—————————
// 2a

var ba = b*-1
var root = (b*
b)-(4*a*c)
var top = Math.sqrt(root);

var topa = ba+top
var topb = ba-top

var tota = topa/(2*a)
var totb = topb/(2*
a)

document.Input.answer1.value = tota;
document.Input.answer2.value = totb;
}

</script>

<form name=”Input”>
<input type=”text” name=”a” value=”a” size=”2″>x<sup>2</sup>+
<input type=”text” name=”b” value=”b” size=”2″>x+
<input type=”text” name=”c” value=”c” size=”2″>=<br /><br />
<input type=”button” onClick=”Calculate()” value=”Calculate X Intercepts”><br />Answers:<br />
<input type=”text” name=”answer1″ size=”6″>&nbsp
<input type=”text” name=”answer2″ size=”6″>

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@SpectreReturnsApr 13.2005 — If the numbers should always be positive (I don't know quadratics), then you can run them through Math.abs(), which returns a positive number from a negetive, and does nothing to a positive.
Copy linkTweet thisAlerts:
@JuuitchanApr 13.2005 — The script is right, and you are wrong.

The ROOTS of a quadratic equation are the numbers for which the left hand side (axx+bx+c) adds up to ZERO.
Copy linkTweet thisAlerts:
@dz_boyauthorApr 13.2005 — The script is right, and you are wrong.

The ROOTS of a quadratic equation are the numbers for which the left hand side (axx+bx+c) adds up to ZERO.[/QUOTE]


Could you tell me how to get the right sign in front of the x intercepts? Sometimes they turn out positive when they should be negative, and negative when the should be positive. Should I use math.abs()?

By the way, I have traced the problem to the value for b, which could switch the sign for both answers.
Copy linkTweet thisAlerts:
@jalarieApr 13.2005 — Juuitchan is correct; the roots of the given example are -3 and -7, not 3 and 7.
Copy linkTweet thisAlerts:
@BigMoosieApr 13.2005 — Your code works fine, if you want to keep the answer in surd form you can use this script I made a while ago:

[COLOR=Red][SIZE=4]Be sure to remove the arbitary spaces that these forums insist upon placing in my script![/SIZE][/COLOR]
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Quadratic Equation Solver&lt;/title&gt;
&lt;script&gt;
function solveEquation(){
var a=document.forms.quadratic.Qa.value;
var b=document.forms.quadratic.Qb.value;
var c=document.forms.quadratic.Qc.value;

if (a==0) {alert("The coefficient of x² must not be zero."); return}

var rootbitValue=(b*b) - (4*a*c);
var rootbit=' &lt;u&gt; +&lt;/u&gt; &amp;#8730;&lt;span style="text-decoration: overline"&gt; '+rootbitValue+'&lt;/span&gt;'

if (Math.sqrt(rootbitValue)==Math.round(Math.sqrt(rootbitValue)*1000000)/1000000) rootbit=" &lt;u&gt; +&lt;/u&gt; "+Math.sqrt(rootbitValue);

else if (rootbitValue%1==0) for (var i=Math.floor(Math.sqrt(rootbitValue)); i&gt;1; i--) if ((rootbitValue/(i*i))%1==0) {rootbit=" &lt;u&gt; +&lt;/u&gt; "+i+" &amp;#8730;&lt;span style=text-decoration:overline&gt; "+(rootbitValue/(i*i)); break;}



var result="&lt;p&gt;"+a+"x² + "+b+"x + "+c+" = 0&lt;/p&gt;";

result=result+'&lt;table border="0" cellpadding="3" cellspacing="0"&gt;&lt;tr&gt;&lt;td rowspan="2" valign="top"&gt;x =&lt;/td&gt;&lt;td style="border-bottom-style: solid; border-color: #000000; border-width: 1px"&gt;'
result=result+(0-b)+rootbit+'&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align="center"&gt;'+(2*a)+'&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;';

output.innerHTML=result;
}
&lt;/script&gt;
&lt;/head&gt;

&lt;body style="font-family: arial; font-size: 12pt"&gt;

&lt;form name=quadratic&gt;
&lt;p&gt;&lt;input type="text" name="Qa" size="2" value="1"&gt;x² + &lt;input type="text" name="Qb" size="2" value="1"&gt;x + &lt;input type="text" name="Qc" size="2" value="0"&gt; = 0 &lt;input type="button" value="Solve" onclick=solveEquation()&gt;&lt;/p&gt;
&lt;/form&gt;

&lt;hr&gt;

&lt;div id=output&gt;&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;


It will output imaginary numbers but will not simplify the equation. I will work on a better one that gives a simplified answer soon. If you dont need this script real soon I can PM you when I do have it made, can't guarantee haste though.
×

Success!

Help @dz_boy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...