-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (29 loc) · 1.14 KB
/
index.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
//eces interest calc
//tinkering #2
function calculate(){
const totalAmount = document.getElementById("total-amount");
const principalInput = document.getElementById("principal");
const rateInput = document.getElementById("rate");
const yearsInput = document.getElementById("years");
let principal = Number(principalInput.value);
let rate = Number(rateInput.value / 100);
let years = Number(yearsInput.value);
if(principal < 0 || isNaN(principal)){
principal = 0;
principalInput.value = 0;
window.alert("🚨principal amount cannot be negative!🚨");
}
if(rate < 0 || isNaN(rate)){
rate = 0;
rateInput.value = 0;
window.alert("🚨interest rate cannot be negative!🚨");
}
if(years < 0 || isNaN(years)){
years = 0;
yearsInput.value = 0;
window.alert("🚨year cannot be negative!🚨");
}
const result = principal * Math.pow((1 + rate / 1), 1 * years)
totalAmount.textContent = result;
totalAmount.textContent = result.toLocaleString(undefined, {style: "currency", currency: "EUR"});
}