Sum array of rational numbers and returning the result in simplest form in JavaScript

We are required to write a JavaScript function that takes in an array of exactly two subarrays with two numbers each.

Both the subarrays represent a rational number in fractional form. Our function should add the rational numbers and return a new array of two numbers representing the simplest form of the added rational number.

Problem

When adding two fractions like 1/2 + 1/3, we need to:

  • Find a common denominator
  • Add the numerators
  • Simplify the result using the Greatest Common Factor (GCD)

Solution

The mathematical formula for adding fractions is: a/b + c/d = (a×d + c×b) / (b×d)

const arr = [
    [1, 2],  // represents 1/2
    [1, 3]   // represents 1/3
];

const findSum = (arr = []) => {
    // Helper function to find Greatest Common Factor (GCD)
    const gcd = (a, b) => b ? gcd(b, a % b) : a;
    
    if (!arr.length) {
        return null;
    }
    
    // Add fractions: [a,b] + [c,d] = [(a*d + c*b), (b*d)]
    const [numerator, denominator] = arr.reduce(([a, b], [c, d]) => [a*d + c*b, b*d]);
    
    // Find GCD to simplify the fraction
    const commonFactor = gcd(numerator, denominator);
    
    // Return simplified fraction or whole number
    return commonFactor === denominator ? numerator / denominator : [numerator / commonFactor, denominator / commonFactor];
};

console.log(findSum(arr));
[5, 6]

How It Works

Let's trace through the example step by step:

// Step 1: Input fractions 1/2 + 1/3
const fraction1 = [1, 2];  // 1/2
const fraction2 = [1, 3];  // 1/3

// Step 2: Apply formula (1×3 + 1×2) / (2×3)
const numerator = (1 * 3) + (1 * 2);    // 3 + 2 = 5
const denominator = 2 * 3;               // 6

console.log("Before simplification:", [numerator, denominator]);

// Step 3: Find GCD of 5 and 6
function gcd(a, b) {
    return b ? gcd(b, a % b) : a;
}

const commonFactor = gcd(5, 6);
console.log("GCD of 5 and 6:", commonFactor);

// Step 4: Since GCD is 1, fraction is already in simplest form
console.log("Final result:", [5, 6]);
Before simplification: [5, 6]
GCD of 5 and 6: 1
Final result: [5, 6]

Example with Simplification

Here's an example where the result needs simplification:

const fractions = [
    [1, 4],  // 1/4
    [1, 4]   // 1/4
];

const findSum = (arr = []) => {
    const gcd = (a, b) => b ? gcd(b, a % b) : a;
    
    if (!arr.length) return null;
    
    const [numerator, denominator] = arr.reduce(([a, b], [c, d]) => [a*d + c*b, b*d]);
    const commonFactor = gcd(numerator, denominator);
    
    return commonFactor === denominator ? numerator / denominator : [numerator / commonFactor, denominator / commonFactor];
};

console.log("1/4 + 1/4 =", findSum(fractions));

// Another example: 2/6 + 1/3 = 4/6 = 2/3
const example2 = [[2, 6], [1, 3]];
console.log("2/6 + 1/3 =", findSum(example2));
1/4 + 1/4 = [1, 2]
2/6 + 1/3 = [2, 3]

Conclusion

This solution efficiently adds rational numbers by finding a common denominator and simplifying using the GCD algorithm. The function handles both cases where the result is a whole number or remains a fraction in its simplest form.

Updated on: 2026-03-15T23:19:00+05:30

220 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements