-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmoneyMoneyMoney.js
50 lines (34 loc) · 1.8 KB
/
moneyMoneyMoney.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
44
45
46
47
48
49
50
// Money,Money,Money from https://www.codewars.com/kata/money-money-money/javascript
/*
Mr. Scrooge has a sum of money 'P' that he 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
Example:
Let P be the Principal = 1000.00
Let I be the Interest Rate = 0.05
Let T be the Tax Rate = 0.18
Let D be the Desired Sum = 1100.00
After 1st Year -->
P = 1041.00
After 2nd Year -->
P = 1083.86
After 3rd Year -->
P = 1128.30
Thus Mr. Scrooge has to wait for 3 years for the initial pricipal to amount to the desired sum.
Your task is to complete the method provided and return the number of years 'Y' as a whole number 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.
*/
/*eslint-disable curly*/
const calculateYears = (principal, interest, tax, desired) => (
Math.ceil(Math.log(desired / principal) / Math.log(1 + (interest * (1 - tax))))
);
const expect = require('chai').expect;
describe('moneyMoneyMoney', () => {
it('should calculate how many years until principal is >= desired amount', () => {
expect(calculateYears(1000, 0.05, 0.18, 1100)).to.equal(3);
expect(calculateYears(1000, 0.01625, 0.18, 1200)).to.equal(14);
});
it('should return 0 when desired amount is = to original principal', () => {
expect(calculateYears(1000, 0.05, 0.18, 1000)).to.equal(0);
});
});