-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
71 lines (56 loc) · 2.3 KB
/
script.js
File metadata and controls
71 lines (56 loc) · 2.3 KB
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
function compute()
{
//validate principal...make sure it has a positive number before continue with the calculation
if(validatePrincipal()){
//get the principal value
var principal = document.getElementById("principal").value;
//get the rate value
var rate = document.getElementById("rate").value;
//get the No of years
var years = document.getElementById("years").value;
//calculate the interest amount
var interest = principal * years * rate/100;
//calculate the maturity year
var year = new Date().getFullYear() + parseInt(years);
//calculate the total amount on the maturity year
var amount = parseFloat(principal) + parseFloat(interest);
//get the "result" ref
var result = document.getElementById("result");
//display the result
var principalSpan = document.createElement("span");
principalSpan.setAttribute('class', 'yellowBG');
principalSpan.innerText= principal;
var rateSpan = document.createElement("span");
rateSpan.setAttribute('class', 'yellowBG');
rateSpan.innerText= rate +"%";
var amountSpan = document.createElement("span");
amountSpan.setAttribute('class', 'yellowBG');
amountSpan.innerText= amount;
var yearSpan = document.createElement("span");
yearSpan.setAttribute('class', 'yellowBG');
yearSpan.innerText= amount;
var resultContent = `If you deposit ` + principalSpan.outerHTML + `, \<br\> at an interest rate of `
+ rateSpan.outerHTML + ` <br\> You will receive an amount of `
+ amountSpan.outerHTML + `, <br\> in the year ` + yearSpan.outerHTML
+ ` <br\>`
result.innerHTML =resultContent;
}
}
function updateRate(){
//get the rate value
var rateval = document.getElementById("rate").value;
//display the rate on the span
document.getElementById("rate_val").innerHTML=rateval;
}
function validatePrincipal(){
var principal = document.getElementById("principal");
if(principal.value == "" || parseFloat(principal.value) <= 0.0){
if (confirm('Enter a positive number')) {
//set focus on principat input
principal.focus();
result.innerHTML=""; //reset the previously calculated result -- if any
return false;
}
}
return true;
}