Find Common Denominator for 2 Fractions with JavaScript - Simplify & Improve I wrote a JavaScript that takes 2 fractions and finds the common denominator for both. If the user inputs 2/6 and 1/2 --- the script outputs 2/6 + 3/6. I'm interested in feedback on possibly simplifying it or improving it. Thanks.

JS Bin: http://jsbin.com/omelen/1/edit

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;script&gt;
window.onload = function() {

    // Finds the highest common factor of 2 numbers
    function highestCommonFactor(a, b) {
        if (b == 0) {
            return a;
        }

        return highestCommonFactor(b, (a % b));
    }

    // Input fractions to add ////////////////////////////////
    // Fraction 1 = 2/6
    var fraction1Numerator = 2;
    var fraction1Denominator = 6;

    // Fraction 2 = 1/2
    var fraction2Numerator = 1;
    var fraction2Denominator = 2;
    ////////////////////////////////////////////////////////////////

    // Find the highest common factor of both denominators
    var factor = highestCommonFactor(fraction1Denominator, fraction2Denominator); // 2

    if (factor &gt; 1) { // There's a common factor greater than 1
        if (fraction1Denominator &gt; fraction2Denominator) {
            // Find out how many times the factor goes into bigger denominator
            var temp = fraction1Denominator / factor;

            fraction2Numerator  = fraction2Numerator * temp;
            fraction2Denominator = fraction2Denominator * temp;
        } else {
            // Find out how many times the factor goes into bigger denominator
            var temp = fraction2Denominator / factor;

            fraction1Numerator  = fraction1Numerator * temp;
            fraction1Denominator = fraction1Denominator * temp;
        }
    } else { // There's no common factor greater than 1 so we need to multiple each fraction by each others denominators
        // Temp values
        var fraction1NumeratorTemp = fraction1Numerator;
        var fraction1DenominatorTemp = fraction1Denominator;

        fraction1Numerator  = fraction1Numerator * fraction2Denominator;
        fraction1Denominator = fraction1Denominator * fraction2Denominator;

        fraction2Numerator  = fraction2Numerator * fraction1DenominatorTemp;
        fraction2Denominator = fraction2Denominator * fraction1DenominatorTemp;
    }

    // Display solution
    document.getElementById("divSolution").innerText = fraction1Numerator + "/" + fraction1Denominator + " + " + fraction2Numerator + "/" + fraction2Denominator;
}
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div id="divSolution"&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
Here's my attempt to help you.


As a general rule, keep everything as general as you can and don't split your code into different cases unless you have to. From the if (factor &gt;1) part, I knew things could be improved
At the end, the denominators will be the Least Common Multiple of the two initial fractions.
Then, you just need to multiply the numerators by the same amount used to multiply the denominators


Here's the corresponding code.

// Finds the highest common factor of 2 numbers
function highestCommonFactor(a, b) {
    if (b == 0) {
        return a;
    }
    return highestCommonFactor(b, a%b);
}
function leastCommonMultiple(a,b) {
    return a*b/(highestCommonFactor(a,b));
}

// Input fractions to add ////////////////////////////////
// Fraction 1 = 2/6
var fraction1Numerator = 2;
var fraction1Denominator = 6;

// Fraction 2 = 1/2
var fraction2Numerator = 1;
var fraction2Denominator = 2;
////////////////////////////////////////////////////////////////

// Find the highest common factor of both denominators
var commonMultiple = leastCommonMultiple(fraction1Denominator, fraction2Denominator); 

fraction1Numerator   *= (commonMultiple / fraction1Denominator);
fraction2Numerator   *= (commonMultiple / fraction2Denominator);

// Display solution
document.getElementById("divSolution").innerText = fraction1Numerator + "/" + commonMultiple + " + " + fraction2Numerator + "/" + commonMultiple;