
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Function that finds the simplest form of summed fractions in JavaScript
We have an array of arrays like this −
const arr = [[12, 56], [3, 45], [23, 2], [2, 6], [2, 8]];
Note that while the array can have any number of elements, each subarray should strictly contain two numbers.
The two numbers in each subarray represents a fraction. Like the fraction represented by the first subarray is 12/56, by the second is 3/45 and so on.
We are required to write a JavaScript function that takes in one such array and calculates the sum of fractions represented by all the subarrays.
We are required to calculate the sum in fraction form (i.e., without converting them to decimals).
And return the sum as an array of two elements representing the resulting fraction.
Therefore, let’s write the code for this function −
Example
The code for this will be −
const arr = [[12, 56], [3, 45], [23, 2], [2, 6], [2, 8]]; const gcd = (a, b) => { let num = 2, res = 1; while(num >= Math.min(a, b)){ if(a % num === 0 && b % num === 0){ res = num; }; num++; }; return res; } const sumFrac = (a, b) => { const aDenom = a[1], aNumer = a[0]; const bDenom = b[1], bNumer = b[0]; let resDenom = aDenom * bDenom; let resNumer = (aDenom*bNumer) + (bDenom*aNumer); const greatestDivisor = gcd(resDenom, resNumer); return [resNumer/greatestDivisor, resDenom/greatestDivisor]; }; const sumArrayOfFractions = arr => { return arr.reduce((acc, val) => sumFrac(acc, val)); }; console.log(sumArrayOfFractions(arr));
Output
The output in the console will be −
[ 1731, 140 ]
- Related Articles
- How to convert fractions into simplest form?
- Convert the following fractions into simplest form with numerator 108 and denominator 132.
- Which of the following fractions are in simplest form (i) $\frac{21}{40}$ (ii) $\frac{35}{49}$
- Recursive product of summed digits JavaScript
- Express The Fraction In Simplest Form.
- Sum array of rational numbers and returning the result in simplest form in JavaScript
- Show that the following fraction is in the simplest form:$\frac{9}{14}$
- JavaScript function that takes a multidimensional and a single array, and finds matches of the single array in the multi-d array
- Find the simplest form of:$\frac{91}{117}$
- Write the simplest form of $ \frac{4}{25}$?
- Express the following in simplest form :$72 : 80$
- Find the simplest form of $90 cm : 1.5 m$
- What is the simplest form of the ratio $120:75$?
- Find the Sum of fractions - JavaScript
- Product of given N fractions in reduced form in C

Advertisements