Skip to content

Instantly share code, notes, and snippets.

@DrStrangeLove
Created June 3, 2012 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrStrangeLove/2865350 to your computer and use it in GitHub Desktop.
Save DrStrangeLove/2865350 to your computer and use it in GitHub Desktop.
Quadratic Equation Solver
function quadraticEquation(ar){
var d;
d = Math.pow(ar[1],2)-(4*ar[0]*ar[2]);
if (d<0){
alert("No Solutions!!");
} else if (d>0){
var res1 = (- (ar[1])- Math.sqrt(d))/(2*ar[0]);
var res2 = (- (ar[1])+ Math.sqrt(d))/(2*ar[0]);
var o = {};
o["x1"] = res1;
o["x2"] = res2;
alert(JSON.stringify(o));
}
if (d===0){
var res = (- (ar[1]))/(2*ar[0]);
alert(res);
}
}
quadraticEquation([8,-14,5]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment