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<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd><html xmlns=http://www.w3.org/1999/xhtml><head><meta http-equiv=Content-Type content=text/html; charset=UTF-8 /><title>Untitled Document</title><script>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 > 1) { // There's a common factor greater than 1        if (fraction1Denominator > 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;}</script></head><body><div id=divSolution></div></body></html>
Find Common Denominator for 2 Fractions with JavaScript - Simplify & Improve
javascript
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 >1) part, I knew things could be improvedAt 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 denominatorsHere's the corresponding code.// Finds the highest common factor of 2 numbersfunction 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/6var fraction1Numerator = 2;var fraction1Denominator = 6;// Fraction 2 = 1/2var fraction2Numerator = 1;var fraction2Denominator = 2;////////////////////////////////////////////////////////////////// Find the highest common factor of both denominatorsvar commonMultiple = leastCommonMultiple(fraction1Denominator, fraction2Denominator); fraction1Numerator   *= (commonMultiple / fraction1Denominator);fraction2Numerator   *= (commonMultiple / fraction2Denominator);// Display solutiondocument.getElementById(divSolution).innerText = fraction1Numerator + / + commonMultiple +  +  + fraction2Numerator + / + commonMultiple;