-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (30 loc) · 1.46 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
let stockData = [];
function addStock() {
const stockPriceInput = document.getElementById('stockPrice');
const quantityInput = document.getElementById('quantity');
const stockList = document.getElementById('stockList');
const averageDisplay = document.getElementById('average');
const stockPrice = parseFloat(stockPriceInput.value);
const quantity = parseInt(quantityInput.value);
if (!isNaN(stockPrice) && !isNaN(quantity) && stockPrice > 0 && quantity > 0) {
const totalValue = stockPrice * quantity;
stockData.push({ stockPrice, quantity, totalValue });
const formatter = new Intl.NumberFormat('en-IN', { style: 'currency', currency: 'INR' });
const li = document.createElement('li');
li.innerText = `${quantity} shares @ ${formatter.format(stockPrice)} each (Total: ${formatter.format(totalValue)})`;
stockList.appendChild(li);
const average = calculateAverage();
averageDisplay.innerText = formatter.format(average);
stockPriceInput.value = '';
quantityInput.value = '';
} else {
alert('Please enter valid values for stock price and quantity.');
}
}
function calculateAverage() {
const totalValue = stockData.reduce((total, data) => total + data.totalValue, 0);
const totalQuantity = stockData.reduce((total, data) => total + data.quantity, 0);
return totalValue / totalQuantity;
}
// Make sure to call calculateAverage initially to set the initial value
calculateAverage();