usingMaths.com
Demonstrating and showing pupils and students one application of Mathematics.







<< Previous Next >>

Code for Subtracting Fractions in JavaScript



Rationalise (Canonise) the Fractions before Subtraction

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


Steps for Subtraction of Fractions JavaScript code

The following steps will guide us in writing our code.
Let's illustrate with 7/4 - 2/5

Step 1:

Find the LCM of the denominators.
         ⇒ LCM of 4 & 5 = 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.
         ⇒ ((7 x 5) - (2 x 4))/20
         = (35 - 8)/20

Step 3:

Go ahead and subtract the numerators.
         ⇒ 27/20


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


So!

Feel free to try out your own set of numerators and denominators for fractional subtraction.









<< Previous Next >>