-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
41 lines (32 loc) · 1.18 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
const initialPrice = document.querySelector("#initial-price");
const stocksQuantity = document.querySelector("#stocks-quantity");
const currentPrice = document.querySelector("#current-price");
const submitBtn = document.querySelector("#check-button");
const outputBox = document.querySelector("#output-box");
submitBtn.addEventListener("click", submitHandler);
function submitHandler() {
let ip = Number(initialPrice.value);
let qty = Number(stocksQuantity.value);
let curr = Number(currentPrice.value);
calculateProfitAndLoss(ip, qty, curr);
}
function calculateProfitAndLoss(initial, quantity, current) {
if (initial > current) {
let loss = (initial - current) * quantity;
let lossPercentage = (loss / initial) * 100;
showOutput(
`Hey, the loss is ${loss} and the percent is ${lossPercentage}%`
);
} else if (current > initial) {
let profit = (current - initial) * quantity;
let profitPercentage = (profit / initial) * 100;
showOutput(
`Hey, the profit is ${profit} and the percent is ${profitPercentage}%`
);
} else {
showOutput(`hay! Nothing in your plate`);
}
}
function showOutput(message) {
outputBox.innerHTML = message;
}