-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (56 loc) · 2.19 KB
/
index.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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");
submitBtn.addEventListener("click", submitHandler);
initialPrice.onkeydown = function(e) {
if(!((e.keyCode > 95 && e.keyCode < 106)
|| (e.keyCode > 47 && e.keyCode < 58)
|| (e.keyCode == 8)
|| (e.keyCode == 110)
|| e.keyCode == 190)) {
return false;
}
}
stocksQuantity.onkeydown = function(e) {
if(!((e.keyCode > 95 && e.keyCode < 106)
|| (e.keyCode > 47 && e.keyCode < 58)
|| e.keyCode == 8)) {
return false;
}
}
currentPrice.onkeydown = function(e) {
if(!((e.keyCode > 95 && e.keyCode < 106)
|| (e.keyCode > 47 && e.keyCode < 58)
|| (e.keyCode == 8)
|| (e.keyCode == 110)
|| e.keyCode == 190)) {
return false;
}
}
function submitHandler() {
var ip = Number(initialPrice.value);
var qty = Number(stocksQuantity.value);
var curr = Number(currentPrice.value);
calculateProfitAndLoss(ip, qty, curr);
}
function decimal (x) {
return Number.parseFloat(x).toFixed(2);
}
function calculateProfitAndLoss(initial, quantity, current) {
if (initial > current) {
var loss = decimal((initial * quantity) - (current * quantity));
var lossPercentage = decimal((loss / (current * quantity)) * 100);
outputBox.textContent = (`Hey the invested amount is ${(initial * quantity)} and loss is ${loss} and the percent is ${lossPercentage}%`)
outputBox.style.color = "red"
} else if (current > initial) {
var profit = decimal((current * quantity) - (initial * quantity));
var profitPercentage = decimal((profit / (initial * quantity)) * 100);
outputBox.textContent = (`Hey the invested amount is ${(initial * quantity)} profit is ${profit} and the percent is ${profitPercentage}%`)
outputBox.style.color = "green"
} else {
outputBox.textContent = (`No Pain No Gain and No Gain No Pain`);
outputBox.style.color = "white"
}
}