-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGRPA1.js
107 lines (93 loc) · 3.49 KB
/
GRPA1.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* calculateSimpleInterest calculates and returns the simple interest
* (floor value) for a fixed deposit. Formula used is,
* calculateSimpleInterest calculates and returns the simple interest
* for a fixed deposit. Formula used is,
* Simple Interest: P X R X T / 100
* where:
* P = Principal
* I = Daily interest rate
* N = Number of days
*
* In case of any input error (wrong date format, alphabets in daily interest etc.), return -1
*
* @param {number} principal - Principal amount
* @param {number} dailyInterest - daily interest rate
* @param {string} startingDate - Starting date of the fixed deposit in "YYYY-MM-DD" format, example "2015-03-25"
* @param {string} endingDate - Ending date of the fixed deposit in "YYYY-MM-DD" format, example "2015-03-25"
* @return {number} interest
*/
/*
* calculateCompoundInterest calculates and returns the compound interest
* (floor value) for a fixed deposit. Formula used is,
* Compound Interest=P[(1+I/100)^N - 1]
* where:
* P = Principal
* I = Daily interest rate
* N = Number of days
*
* In case of any input error (wrong date format, alphabets in daily interest etc.), return -1
*
* @param {number} principal - Principal amount
* @param {number} dailyInterest - daily interest rate
* @param {string} startingDate - Starting date of the fixed deposit in "YYYY-MM-DD" format, example "2015-03-25"
* @param {string} endingDate - Ending date of the fixed deposit in "YYYY-MM-DD" format, example "2015-03-25"
* @return {number} interest
*/
/*
* extraAmountPercentage calculates and returns the extra amount percentage borrower will have to pay in case of
* compound interest (floor value) in comparison to the simple interest for a fixed deposit.
* In case of any input error (wrong date format, alphabets in daily interest etc.), return -1
*
* @param {number} principal - Principal amount
* @param {number} dailyInterest - Daily interest rate.
* @param {string} startingDate - Starting date of the fixed deposit in "YYYY-MM-DD" format, example "2015-03-25"
* @param {string} endingDate - Ending date of the fixed deposit in "YYYY-MM-DD" format, example "2015-03-25"
* @return {number} percentage
*/
function calculateSimpleInterest(
principal,
dailyInterest,
startingDate,
endingDate
) {
s = new Date(startingDate);
e = new Date(endingDate);
//console.log(s, "\n", e);
if (s == "Invalid Date") {return -1;}
if (e == "Invalid Date") {return -1;}
diff = (e.getTime() - s.getTime()) / (1000 * 60 * 60 * 24);
result = Number(principal) * Number(dailyInterest) * Number(diff) / 100;
return Math.floor(result);
}
function calculateCompoundInterest(
principal,
dailyInterest,
startingDate,
endingDate
) {
s = new Date(startingDate);
e = new Date(endingDate);
if (s == "Invalid Date") {return -1;}
if (e == "Invalid Date") {return -1;}
diff = (e.getTime() - s.getTime()) / (1000 * 60 * 60 * 24);
result = principal * (Math.pow((1 + dailyInterest / 100), diff) - 1);
return Math.floor(result);
}
function extraAmountPercentage(principal, dailyInterest, startingDate, endingDate) {
compound = calculateCompoundInterest(
principal,
dailyInterest,
startingDate,
endingDate
);
simple = calculateSimpleInterest(
principal,
dailyInterest,
startingDate,
endingDate
);
if (compound == -1) {return -1;}
if (simple == -1) {return -1;}
return Math.floor((compound - simple) / simple * 100);
}