-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Arithmetic Geometric Calculator #987
- Loading branch information
1 parent
3c1358d
commit 693cc61
Showing
6 changed files
with
459 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+292 KB
Calculators/Arithmetic-Geometric-Progression-Calculator/assets/background.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 33 additions & 7 deletions
40
Calculators/Arithmetic-Geometric-Progression-Calculator/script.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,57 @@ | ||
function calculate() { | ||
// Get input values | ||
const firstTerm = parseFloat(document.getElementById('firstTerm').value); | ||
const commonDifference = parseFloat(document.getElementById('commonDifference').value); | ||
const commonRatio = parseFloat(document.getElementById('commonRatio').value); | ||
const termNumber = parseInt(document.getElementById('termNumber').value); | ||
const commonDifference = parseInt(document.getElementById('commonDifference').value); | ||
const calculationType = document.getElementById('calculationType').value; | ||
|
||
|
||
const n = termNumber; | ||
const a = firstTerm; | ||
const d = commonDifference; | ||
const r = commonRatio; | ||
if (isNaN(a) || isNaN(d) || isNaN(n) || isNaN(r)) { | ||
document.getElementById("result").innerText = "Please enter the valid numbers for all fields."; | ||
return; | ||
|
||
} | ||
// Perform the selected calculation | ||
let result; | ||
if (calculationType === 'nthTerm') { | ||
// Calculate the nth term | ||
result = calculateNthTerm(firstTerm, commonDifference, termNumber); | ||
result = calculateNthTerm(firstTerm, commonRatio, termNumber, commonDifference); | ||
} else if (calculationType === 'sumOfTerms') { | ||
// Calculate the sum of the first n terms | ||
result = calculateSumOfTerms(firstTerm, commonDifference, termNumber); | ||
result = calculateSumOfTerms(firstTerm, commonRatio, termNumber, commonDifference); | ||
} | ||
|
||
// Display the result | ||
const resultElement = document.getElementById('result'); | ||
resultElement.textContent = result; | ||
} | ||
|
||
function calculateNthTerm(firstTerm, commonDifference, termNumber) { | ||
function calculateNthTerm(firstTerm, commonRatio, termNumber, commonDifference) { | ||
// Calculate the nth term | ||
return `The ${termNumber}th term is: ${firstTerm + (termNumber - 1) * commonDifference}`; | ||
const n = termNumber; | ||
const a = firstTerm; | ||
const d = commonDifference; | ||
const r = commonRatio; | ||
const m = (Math.pow(commonRatio, termNumber - 1)); | ||
|
||
return `The ${termNumber}th term is: ${(a + (n - 1) * d) * m}`; | ||
} | ||
|
||
function calculateSumOfTerms(firstTerm, commonDifference, termNumber) { | ||
function calculateSumOfTerms(firstTerm, commonRatio, termNumber, commonDifference) { | ||
// Calculate the sum of the first n terms | ||
return `The sum of the first ${termNumber} terms is: ${(termNumber / 2) * (2 * firstTerm + (termNumber - 1) * commonDifference)}`; | ||
const n = termNumber; | ||
const a = firstTerm; | ||
const d = commonDifference; | ||
const r = commonRatio; | ||
const p = 1 - commonRatio; | ||
const l = (1 - Math.pow(commonRatio, termNumber - 1)); | ||
const m = (Math.pow(commonRatio, termNumber)); | ||
const w = Math.pow(p, 2); | ||
|
||
return `The sum of the first ${termNumber} terms is: ${(a / p) + ((d * r * l) / w) - (((a + (n - 1) * d) * m)) / p}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.