-
Notifications
You must be signed in to change notification settings - Fork 0
/
Discount.js
47 lines (42 loc) · 1.49 KB
/
Discount.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
// console.log((20 * 10) / 100);
let inputPrice = document.querySelector(".valueInput");
let discountPrice = document.querySelector(".discountInput");
let discountOutAmount = document.querySelector(".discountOutput");
let finalOutAmount = document.querySelector(".finalOutput");
let convertBtn = document.querySelector(".Convert");
let resetBtn = document.querySelector(".Reset");
let alert = document.querySelector(".alertMsg");
let closeAlert = document.querySelector(".fa-times");
function calc() {
if (discountPrice.value > 100) {
// To avoid the calculation for values more than 100%
alert.style.display = "grid";
// alert("Discount price exceeds the limit");
} else {
let evalFinalOutput = eval((inputPrice.value * discountPrice.value) / 100);
let calcDiscountAmount = inputPrice.value - evalFinalOutput;
finalOutAmount.innerHTML = `₹ ${calcDiscountAmount}`;
discountOutAmount.innerHTML = `₹ ${evalFinalOutput}`;
}
}
closeAlert.addEventListener("click", () => {
alert.style.display = "none";
});
resetBtn.addEventListener("click", () => {
inputPrice.value = "";
discountPrice.value = "";
finalOutAmount.innerHTML = "";
discountOutAmount.innerHTML = "";
});
// Using the Enter key to trigger the output
[inputPrice, discountPrice].forEach((item) =>
item.addEventListener("keypress", (e) => {
if (e.key === "Enter") {
calc();
}
// Disabling the use of '-' & '+' key
if (e.key === "-" || e.key === "+") {
e.preventDefault();
}
})
);