Код: Выделить всё
let balance = 10000;
let stocks = [];
// Function to fetch stock price using Alpha Vantage API
async function getStockPrice(symbol) {
try {
const apiKey = 'YOUR_API_KEY'; // Replace 'YOUR_API_KEY' with your actual Alpha Vantage API key
const response = await fetch(`https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=${symbol}&apikey=${apiKey}`);
const data = await response.json();
// Extract the latest stock price from the API response
const latestPrice = parseFloat(data['Global Quote']['05. price']);
return latestPrice;
} catch (error) {
console.error('Error fetching stock price:', error);
return null;
}
}
function buyStock(event) {
event.preventDefault(); // Prevent form submission
const stockSymbol = document.getElementById("stockInput").value.toUpperCase();
const quantity = parseInt(document.getElementById("quantityInput").value);
getStockPrice(stockSymbol).then(stockPrice => {
const totalCost = stockPrice * quantity;
// Check if the user has enough balance to buy stocks
if (totalCost {
const option = document.createElement("option");
option.value = stock.symbol;
option.textContent = `${stock.quantity} shares of ${stock.symbol}`;
sellDropdown.appendChild(option);
});
}
function findStockIndex(symbol) {
for (let i = 0; i < stocks.length; i++) {
if (stocks[i].symbol === symbol) {
return i;
}
}
return -1; // Stock symbol not found
}
document.getElementById("buyForm").addEventListener("submit", buyStock);
document.getElementById("sellForm").addEventListener("submit", sellStock);
Код: Выделить всё
body {
font-family: 'Roboto Mono', monospace;
color: #fff;
background-color: #000;
margin: 0;
padding: 0;
}
header {
background-color: #333;
color: #fff;
padding: 20px;
text-align: center;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
h1 {
margin: 0;
font-size: 36px;
}
section {
padding: 20px;
background-color: #222;
margin: 20px auto;
max-width: 600px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
h2 {
margin-top: 0;
font-size: 24px;
color: #fff;
}
form {
max-width: 400px;
margin: 0 auto;
}
label {
margin-bottom: 10px;
font-size: 16px;
}
input[type="text"],
input[type="number"],
button {
margin-bottom: 20px;
padding: 12px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 4px;
background-color: #333;
color: #fff;
transition: border-color 0.3s ease;
}
input[type="text"]:focus,
input[type="number"]:focus,
button:focus {
outline: none;
border-color: #007bff;
}
button {
cursor: pointer;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 4px;
padding: 12px 20px;
font-size: 18px;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #0056b3;
}
#stockList {
list-style: none;
padding: 0;
}
footer {
background-color: #333;
color: #fff;
text-align: center;
padding: 20px;
position: fixed;
bottom: 0;
width: 100%;
}
Код: Выделить всё
Stock Market Simulator
Your Balance: $10000
Buy Stocks
Stock Symbol:
Quantity:
Buy
Your Stocks
[list]
[/list]
Sell Stocks
© 2024 Stock Market Simulator
Я пробовал действия по устранению неполадок в соответствии со стеком и чатгптом, но ничего не помогает< /п>
Подробнее здесь: https://stackoverflow.com/questions/784 ... -in-chrome