-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
50 lines (42 loc) · 1.54 KB
/
script.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
function compute()
{
// Get the values entered by the user
var principal = document.getElementById("principal").value;
var rate = document.getElementById("rate").value;
var years = document.getElementById("years").value;
// Compute interest and year from user values
var interest = principal * years * rate / 100;
var year = new Date().getFullYear() + parseInt(years);
// Validate that the values entered in the form are valid
var is_valid = validateForm();
// If the form is invalid, return
if (!is_valid){
return;
}
// Update result HTML accordingly
document.getElementById("result").innerHTML = `
<br>
If you deposit <mark>${principal}</mark><br>
at an interest rate of <mark>${rate}%</mark><br>
You will receive an amount of <mark>${interest}</mark><br>
in the year <mark>${year}</mark>
<br>`;
}
function validateForm(){
// Validate that the values entered by the user are valid
let principal_element = document.getElementById("principal");
// Check that the principal value is a strictly positive number
if (principal_element.value <= 0) {
alert("Enter a positive number");
// Set the focus on the principal value if invalid
principal_element.focus();
return false;
}
// If form is valid, return true
return true;
}
function updateRate(){
// Update the rate value according to the slider bar value
var rateval = document.getElementById("rate").value;
document.getElementById("rate_val").innerText = rateval + "%";
}