function gcd(a, b) {
a = Math.abs(a);
b = Math.abs(b);
while (b) {
var temp = b;
b = a % b;
a = temp;
}
return a;
}
function addFractions(n1, d1, n2, d2) {
var resultNum = (n1 * d2) + (n2 * d1);
var resultDen = d1 * d2;
return [resultNum, resultDen];
}
function subtractFractions(n1, d1, n2, d2) {
var resultNum = (n1 * d2) – (n2 * d1);
var resultDen = d1 * d2;
return [resultNum, resultDen];
}
function multiplyFractions(n1, d1, n2, d2) {
var resultNum = n1 * n2;
var resultDen = d1 * d2;
return [resultNum, resultDen];
}
function divideFractions(n1, d1, n2, d2) {
if (n2 === 0) {
return "Error: Cannot divide by a fraction with a zero numerator.";
}
var resultNum = n1 * d2;
var resultDen = d1 * n2;
return [resultNum, resultDen];
}
function calculateFractions() {
var num1 = parseFloat(document.getElementById("num1").value);
var den1 = parseFloat(document.getElementById("den1").value);
var operation1 = document.getElementById("operation1").value;
var num2 = parseFloat(document.getElementById("num2").value);
var den2 = parseFloat(document.getElementById("den2").value);
var operation2 = document.getElementById("operation2").value;
var num3 = parseFloat(document.getElementById("num3").value);
var den3 = parseFloat(document.getElementById("den3").value);
var resultDiv = document.getElementById("result");
resultDiv.innerHTML = ""; // Clear previous result
// Input validation
if (isNaN(num1) || isNaN(den1) || isNaN(num2) || isNaN(den2) || isNaN(num3) || isNaN(den3)) {
resultDiv.innerHTML = "Please enter valid numbers for all numerators and denominators.";
return;
}
if (den1 === 0 || den2 === 0 || den3 === 0) {
resultDiv.innerHTML = "Denominator cannot be zero.";
return;
}
if (den1 < 0 || den2 < 0 || den3 < 0) {
resultDiv.innerHTML = "Denominator cannot be negative. Please enter a positive denominator.";
return;
}
var intermediateResult;
// Perform first operation: (Fraction 1 Op1 Fraction 2)
switch (operation1) {
case "add":
intermediateResult = addFractions(num1, den1, num2, den2);
break;
case "subtract":
intermediateResult = subtractFractions(num1, den1, num2, den2);
break;
case "multiply":
intermediateResult = multiplyFractions(num1, den1, num2, den2);
break;
case "divide":
intermediateResult = divideFractions(num1, den1, num2, den2);
break;
}
if (typeof intermediateResult === 'string') { // Error from divideFractions
resultDiv.innerHTML = "" + intermediateResult + "";
return;
}
var currentNum = intermediateResult[0];
var currentDen = intermediateResult[1];
// Perform second operation: (intermediateResult Op2 Fraction 3)
var finalResult;
switch (operation2) {
case "add":
finalResult = addFractions(currentNum, currentDen, num3, den3);
break;
case "subtract":
finalResult = subtractFractions(currentNum, currentDen, num3, den3);
break;
case "multiply":
finalResult = multiplyFractions(currentNum, currentDen, num3, den3);
break;
case "divide":
finalResult = divideFractions(currentNum, currentDen, num3, den3);
break;
}
if (typeof finalResult === 'string') { // Error from divideFractions
resultDiv.innerHTML = "" + finalResult + "";
return;
}
var finalNum = finalResult[0];
var finalDen = finalResult[1];
// Simplify the final fraction
if (finalNum === 0) {
resultDiv.innerHTML = "The result is: 0";
return;
}
var commonDivisor = gcd(finalNum, finalDen);
var simplifiedNum = finalNum / commonDivisor;
var simplifiedDen = finalDen / commonDivisor;
// Handle negative denominators by moving sign to numerator
if (simplifiedDen < 0) {
simplifiedNum = -simplifiedNum;
simplifiedDen = -simplifiedDen;
}
// Display result
if (simplifiedDen === 1) {
resultDiv.innerHTML = "The result is: " + simplifiedNum + "";
} else {
resultDiv.innerHTML = "The result is: " + simplifiedNum + " / " + simplifiedDen + "";
}
}
Understanding the 3 Fractions Calculator
Fractions are fundamental mathematical concepts representing parts of a whole. They are written as a ratio of two numbers, a numerator (the top number) and a denominator (the bottom number). This 3 Fractions Calculator allows you to perform sequential arithmetic operations (addition, subtraction, multiplication, and division) on three different fractions, providing the simplified result.
What is a Fraction?
A fraction consists of two main parts:
Numerator: The number above the line, indicating how many parts of the whole are being considered.
Denominator: The number below the line, indicating the total number of equal parts the whole is divided into. The denominator can never be zero.
For example, in the fraction 1/2, '1' is the numerator and '2' is the denominator, meaning one part out of two equal parts.
How to Perform Operations on Fractions
1. Addition and Subtraction
To add or subtract fractions, they must have a common denominator. If they don't, you find the least common multiple (LCM) of the denominators to create equivalent fractions. Once they have the same denominator, you simply add or subtract the numerators and keep the denominator the same.
Formula: a/b + c/d = (ad + bc) / bd
Formula: a/b - c/d = (ad - bc) / bd
2. Multiplication
Multiplying fractions is straightforward: multiply the numerators together and multiply the denominators together.
Formula: a/b * c/d = (a * c) / (b * d)
3. Division
To divide fractions, you "invert" (flip) the second fraction (the divisor) and then multiply it by the first fraction.
Formula: a/b / c/d = a/b * d/c = (a * d) / (b * c)
Simplifying Fractions
After performing operations, the resulting fraction often needs to be simplified to its lowest terms. This means finding the greatest common divisor (GCD) of the numerator and the denominator and dividing both by it. For example, 2/4 simplifies to 1/2 because the GCD of 2 and 4 is 2.
How to Use This Calculator
Enter Fraction 1: Input the numerator and denominator for your first fraction.
Select Operation 1: Choose the arithmetic operation (+, -, *, /) you want to perform between Fraction 1 and Fraction 2.
Enter Fraction 2: Input the numerator and denominator for your second fraction.
Select Operation 2: Choose the arithmetic operation (+, -, *, /) you want to perform between the result of the first operation and Fraction 3.
Enter Fraction 3: Input the numerator and denominator for your third fraction.
Click "Calculate": The calculator will perform the operations sequentially and display the simplified final fraction.
This calculator is a valuable tool for students, educators, and anyone needing to quickly and accurately perform operations on multiple fractions. It helps:
Save Time: Automates complex fraction arithmetic.
Reduce Errors: Minimizes the chance of calculation mistakes.
Verify Solutions: Allows you to check your manual calculations.
Learn and Practice: Provides immediate feedback for understanding fraction operations.