-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.js
42 lines (35 loc) · 1.23 KB
/
main.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
// Step 1 - selecting all the elements
var initialPrice = document.querySelector("#initial-price");
var stocksQuantity = document.querySelector("#stocks-quantity");
var currentPrice = document.querySelector("#current-price");
var submitBtn = document.querySelector("#submit-btn");
var outputBox = document.querySelector("#output-box");
// Ex-06
submitBtn.addEventListener("click", submitHandler);
function submitHandler() {
var ip = Number(initialPrice.value);
var qty = Number(stocksQuantity.value);
var curr = Number(currentPrice.value);
calculateProfitAndLoss(ip, qty, curr);
}
// Ex-05
function calculateProfitAndLoss(initial, quantity, current) {
if (initial > current) {
var loss = (initial - current) * quantity;
var lossPercentage = (loss / initial) * 100;
showOutput(
`Hey, the loss is ${loss} and the percent is ${lossPercentage}%`
);
} else if (current > initial) {
var profit = (current - initial) * quantity;
var profitPercentage = (profit / initial) * 100;
showOutput(
`Hey, the profit is ${profit} and the percent is ${profitPercentage}%`
);
} else {
showOutput(`No pain no gain and no gain no pain`);
}
}
function showOutput(message) {
outputBox.innerHTML = message;
}