# Solve Quadratic Equations with the Formula

**URL:** <https://forum.kirupa.com/t/solve-quadratic-equations-with-the-formula/169327>\
**Category:** open source\
**Created:** [November 15, 2005, 1:13pm UTC](https://forum.kirupa.com/t/solve-quadratic-equations-with-the-formula/169327 "2005-11-15T13:13:25Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sammo](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sammo/32/779_2.png) [@Sammo](https://forum.kirupa.com/u/Sammo)\
**Post date:** [November 15, 2005, 1:13pm UTC](https://forum.kirupa.com/t/solve-quadratic-equations-with-the-formula/169327/1 "2005-11-15T13:13:25Z")

</div>

This is a function I wrote a while ago to save me the time of pounding it into a calculator over and over again during a Maths homework.

```auto
//////////////////////////////////////////
////// Function to solve quadratics //////
////// by the eqaution: //////
////// //////
////// -b ± sqrt(b² - 4ac) //////
////// ———————————————— //////
////// 2a //////
////// //////
////// By Sam Kellett //////
////// ([http://cannedlaughter.net](http://cannedlaughter.net)) //////

function quadratic(a,b,c) {
    step1a = -b + Math.sqrt((b*b) - (4 * a * c));
    trace("step 1a: "+step1a);
    step2a = step1a/(2*a);
    trace("step 2a: "+step2a);
    
    step1b = -b - Math.sqrt((b*b) - 4 * a * c);
    trace("step 1b: "+step1b);
    step2b = step1b/(2*a);
    trace("step 2b: "+step2b);
    
    var x:Array = new Array(step2a,step2b);
    return x;
}

// if you had x² - 3x - 4
// We take a, b & c from ax² + bx + cx
// so a = 1, b = -3 and c = -4 
// you would call the function like so:
example = quadratic(1,-3,-4);

// the variable example will now be an array 
// containing the two possible answers produced 
// by the formula
trace(example);
// returns:
// "4,-1"

```
