-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculateInvestmentResults.js
43 lines (40 loc) · 1.49 KB
/
calculateInvestmentResults.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* The function to calculate investment results
* based on parameters listed below
*
* @param initialInvestment {number} - The initial investment amount
* @param annualInvestment {number} - The amount invested every year
* @param expectedReturn {number} - The expected (annual) rate of return
* @param duration {number} - The investment duration (time frame)
*
* @returns annualData {[]} - The array of objects with the investment results
*/
export function calculateInvestmentResults(
initialInvestment,
annualInvestment,
expectedReturn,
duration,
) {
const annualData = [];
let investmentValue = initialInvestment;
for (let i = 0; i < duration; i++) {
const interestEarnedInYear = investmentValue * (expectedReturn / 100);
investmentValue += interestEarnedInYear + annualInvestment;
annualData.push({
year: i + 1, // year identifier
interest: interestEarnedInYear, // the amount of interest earned in this year
valueEndOfYear: investmentValue, // investment value at end of year
annualInvestment: annualInvestment, // investment added in this year
});
}
return annualData;
}
// The browser-provided Intl API is used to prepare a formatter object
// This object offers a "format()" method that can be used to format numbers as currency
// Example Usage: formatter.format(1000) => yields "$1,000"
export const formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 0,
maximumFractionDigits: 0,
});