forked from 0xvashishth/CalcHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
53 lines (45 loc) · 1.7 KB
/
app.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
let initialPrice = document.querySelector("#initial-price");
let stockQuantity = document.querySelector("#stock-quantity");
let sellingPrice = document.querySelector("#selling-price");
let submitBtn = document.querySelector("#submit-btn");
let outputBox = document.querySelector("#output-box");
submitBtn.addEventListener("click", submitHandler);
// selling-price -> sellingPrice
function submitHandler() {
let ip = Number(initialPrice.value);
let qty = Number(stockQuantity.value);
let sellPrice = Number(sellingPrice.value);
// curr -> sellPrice
calculateProfitAndLoss(ip, qty, sellPrice);
}
function calculateProfitAndLoss(initial, quantity, current) {
if (initial === "" || quantity === "" || current === "") {
outputBox.innerHTML = "Please enter all the values.<br><br>";
} else if (
Number.parseInt(quantity) <= 0 ||
Number.parseInt(initial) <= 0 ||
Number.parseInt(current) <= 0
) {
outputBox.innerHTML = "Please enter values greater than 0.<br><br>";
} else if (current > initial) {
let profit = ((current - initial) * quantity).toFixed(2);
let profitPercentage = ((current - initial) / (initial / 100)).toFixed(2);
outputBox.innerHTML =
"You gained " +
profitPercentage +
"%</br>Your total profit is ₹ " +
profit +
"<br><br>";
} else if (initial > current) {
let loss = ((initial - current) * quantity).toFixed(2);
let lossPercentage = ((initial - current) / (initial / 100)).toFixed(2);
outputBox.innerHTML =
"You lost " +
lossPercentage +
"%</br>Your total loss is ₹ " +
loss +
"<br><br>";
} else {
outputBox.innerHTML = "You make neither a profit nor a loss. <br><br>";
}
}