Untitled
unknown
javascript
2 years ago
1.0 kB
1
Indexable
Never
let balance = 1000; // initial balance in USD let price = 50; // initial price of the stock in USD let buyThreshold = 40; // buy when the price drops below this threshold let sellThreshold = 60; // sell when the price rises above this threshold function buy() { balance -= price; console.log(`Bought one stock for ${price} USD. Balance: ${balance} USD`); } function sell() { balance += price; console.log(`Sold one stock for ${price} USD. Balance: ${balance} USD`); } function updatePrice(newPrice) { if (newPrice < buyThreshold && balance > price) { price = newPrice; buy(); } else if (newPrice > sellThreshold && balance < price) { price = newPrice; sell(); } else { price = newPrice; console.log(`Price updated to ${price} USD.`); } } // simulate price updates over time updatePrice(45); // buy one stock updatePrice(30); // buy another stock updatePrice(70); // sell one stock updatePrice(80); // sell another stock updatePrice(55); // do nothing