-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
33 lines (27 loc) · 1.01 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
var initialPrice = document.getElementById("initial-price");
var stocksQuantity = document.getElementById("stocks-quantity");
var currentPrice = document.getElementById("current-price");
var calculate = document.getElementById("calculate");
var outputBox = document.getElementById("output-box");
calculate.addEventListener("click", submitHandler);
function submitHandler() {
var ip = Number(initialPrice.value);
var qty = Number(stocksQuantity.value);
var curr = Number(currentPrice.value);
var diff = curr - ip
if (diff < 0) {
var loss = diff * qty * (-1);
var lossPercentage = (diff / ip) * 100 * (-1);
outputBox.innerHTML = (
`Hey, the loss is ${loss} and the percent is ${lossPercentage}%`
);
} else if (diff > 0) {
var profit = diff * qty;
var profitPercentage = (diff / ip) * 100;
outputBox.innerHTML = (
`Hey, the profit is ${profit} and the percent is ${profitPercentage}%`
);
} else {
outputBox.innerHTML = (`No pain no gain and no gain no pain`);
}
}