-
Notifications
You must be signed in to change notification settings - Fork 0
/
Money_Money_Money.js
24 lines (22 loc) · 1.25 KB
/
Money_Money_Money.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
/*
My solution to the Codewars Challenge- Money Money Money in JavaScript.
Mr. Scrooge has a sum of money 'P' that wants to invest, and he wants to know how many years 'Y' this sum has to be kept in the bank in order for this sum of money to amount to 'D'.
The sum is kept for 'Y' years in the bank where interest 'I' is paid yearly, and the new sum is re-invested yearly after paying tax 'T'
Note that the principal is not taxed but only the year's accrued interest
Thus Mr. Scrooge has to wait for 3 years for the initial pricipal to ammount to the desired sum.
Your task is to complete the method provided and return the number of years 'Y' as a whole in order for Mr. Scrooge to get the desired sum.
Assumptions : Assume that Desired Principal 'D' is always greater than the initial principal, however it is best to take into consideration that if the Desired Principal 'D' is equal to Principal 'P' this should return 0 Years.
*/
function calculateYears(principal, interest, tax, desired) {
let accumulation = 0;
let years = 0;
if (desired === principal) {
return 0;
} else {
while ((principal + accumulation) < desired) {
accumulation += (((principal + accumulation) * interest) * (1 - tax));
years++;
}
return years;
}
}