usingMaths.com
From Theory to Practice - Math You Can Use.







<< Previous Next >>

How to Reduce Fractions to their Lowest Term in JavaScript | Kids Fun Project



What is Fraction Reduction to Lowest Term? | Maths Explanation for JavaScript Kids

Reducing fractions to their lowest terms is all about removing every common factor between the numerator and the denominator.

In this junior secondary JavaScript tutorial, you'll learn how to simplify fractions using a custom module. The JavaScript code provided helps reduce fractions to their lowest terms by finding common factors between the numerator and denominator. Whether you're a student or teacher, this math-focused JavaScript project makes fraction simplification easy and fun.


How the Reduce Fraction to Lowest Term Method Works | Detailed Explanation for JavaScript Kids

Let's see a JavaScript algorithm implementation to reduce a fraction to it's lowest term, using 12/16 as an example fraction.

A common factor to 12 and 16 is 2;
so removing 2 from 12/16 yields 6/8.

Again, 2 is common to both 6 and 8;
removing it yields 3/4.

We stop when nothing else is common (1 does not count).

Create a new file; On Notepad++: File, New.
Call it LowestTerm.html.
Type out the adjoining JavaScript code for reducing fractions to their lowest term.


So! JavaScript Fun Practice Exercise - Reduce Fraction to Lowest Term

As a fun practice exercise, feel free to try out your own fractions with different numerators and denominators, and see how the JavaScript code simplifies those fractions.







JavaScript Code for Reducing Fractions to Lowest Term

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Reducing Fractions to their Lowest Terms</title>
    </head>
    <body>

        <h3>Reduce Fractions to their Lowest Terms</h3>
        <!-- This is where the result will be displayed when it is ready.-->
        <div id="lowest_term"></div>

        <script>

            var numerator = 16;
            var denominator = 640;
            var trial_factor;
            // between numerator and denominator take the larger one
            numerator < denominator ? trial_factor = numerator : trial_factor = denominator;

            var result = "Reducing to its lowest term the fraction:<br/>";

            // Print as fraction
            result += "<sup>" + numerator + "</sup> / <sub>" + denominator + "</sub><br/><br/>";

            // Carry out reduction
            // We are counting down to test for mutual factors 
            while (trial_factor > 1) {
                // do we have a factor
                if ((numerator % trial_factor) === 0) {
                    // is this factor mutual?
                    if ((denominator % trial_factor) === 0) {
                        // where we have a mutual factor
                        numerator /= trial_factor;
                        denominator /= trial_factor;
                        continue// next iteration; repeating the current value of trial_factor
                    }
                }
                trial_factor--;
            }

            // Print as fraction
            result += "Answer &nbsp;&nbsp;  =  &nbsp;&nbsp;  ";
            result += " <sup>" + numerator + "</sup> ";
            result += "/ <sub>" + denominator + "</sub><br/>";

            document.getElementById("lowest_term").innerHTML = result;

        </script>

    </body>
</html>




<< Previous Next >>