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







<< Previous Next >>

How to Add Fractions using JavaScript | Step-by-Step Tutorial with Fun Exercises



Why Rationalise or Canonise Fractions before Addition | Maths Explanation for JavaScript Kids

In this JavaScript tutorial for junior secondary students, we explore how to add fractions. Before performing the addition, we rationalise or canonise the fractions to ensure accuracy. This method uses Finding LCM in JavaScript class to align denominators, making it ideal for math programming beginners.
This JavaScript tutorial teaches young students how to add fractions with different denominators.

Before fractions are added, they are rationalised; i.e., they are put in a form where their denominators become the same. This identical denominator is the LCM of the previous denominators of all the separate fractions.
After this is done, the new numerators can then be added together.


Step-by-Step Guide for Addition of Fractions - JavaScript Algorithm

The following steps will guide us in writing our JavaScript code for adding fractions.
Let's illustrate the steps to follow with the example fractional expression 2/5 + 7/4

Step 1:

Using the Find LCM in JavaScript class from the Primary Category, find the LCM of the denominators.
         ⇒ LCM of 5 & 4 = 20

Step 2:

In a turn by turn fashion, divide the found LCM from Step 1 by each denominator, multiplying the quotient by the corresponding numerator.
         ⇒ ((2 x 4) + (7 x 5))/20
         = (8 + 35)/20

Step 3:

Go ahead and add the numerators.
         ⇒ 43/20


Create 2 new files; On Notepad++: File, New.
Call them AddFraction.html and AddFraction.js;
Type out the adjoining JavaScript code for adding fractions.


Note: The code module for Learn how to find LCM in JavaScript has been explained in the Primary Category.
Just make a copy of it to your current directory.


So! JavaScript Fun Practice Exercise - Add Fractions

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







JavaScript Code for AddFraction.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Adding Fractions</title>
        <script src="LCM.js"></script>
        <script src="AddFraction.js"></script>
    </head>
    <body>

        <h3>Add Fractions</h3>
        <!-- This is where the result will be displayed when it is ready.-->
        <div id="add_fraction"></div>

        <script>

            var result = "Solving:<br/>";

            // Print as fraction
            for (var i = 0; i < numerators.length-1; i++) {
                result += "<sup>" + numerators[i] + "</sup> / <sub>" + denominators[i] + "</sub>";
            result += "&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            }
            result += "<sup>" + numerators[i] + "</sup> / <sub>" + denominators[i] + "</sub><br/><br/>";

            // Carry out addition
            doAdd();

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

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

        </script>

    </body>
</html>

JavaScript Code for AddFraction.js

var numerators = [1111];//enter your own numerators
var denominators = [4444];//enter your own denominators
var new_numerators = [];
var answer = 0;
var LCM = 0;

all_factors = [];
state_check = false;
= 2;
index = 0;

function canonizeFraction() {
    // find the LCM of denominators
    // STEP 1:
    LCM = getLCM(denominators);

    // transform fractions so they all have same denominator
    // STEP 2:
    for (var i = 0; i < denominators.length; i++) {
        new_numerators[i] = LCM / denominators[i] * numerators[i];
    }
}

function doAdd() {
    canonizeFraction();

    // add all transformed numerators
    // STEP 3:
    for (var i = 0; i < new_numerators.length; i++) {
        answer += new_numerators[i];
    }
}




<< Previous Next >>