-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathmoney-money-money.js
More file actions
25 lines (22 loc) · 838 Bytes
/
money-money-money.js
File metadata and controls
25 lines (22 loc) · 838 Bytes
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
function calculateYears(principal, interest, tax, desired) {
// if principal is equal to desired
if (principal === desired) {
return 0;
}
// a place to store the number of years - initialized to zero
let years = 0;
// iterate until principal is greater than or equal to the desired
while (principal < desired) {
// calculate interest from principal + total interest gained
let totalInterestGained = principal * interest;
// calcuate tax from total interest gained
let totalTax = totalInterestGained * tax;
principal = principal + (totalInterestGained - totalTax);
// increment number of years
years++;
}
return years;
}
console.log(calculateYears(1000, 0.05, 0.18, 1100), 3);
console.log(calculateYears(1000,0.01625,0.18,1200), 14);
console.log(calculateYears(1000,0.05,0.18,1000), 0);