Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
const auctionItem =
{
name: "Item Name",
description: "Item Description:",
startingBid: 100,
highestBid: 100,
};
let currentHighestBid = 100;
const bidForm = document.querySelector("form");
const bidInput = document.querySelector("#bid-amount");
const currentBidDisplay = document.querySelector("#current-bid");
const highestBidDisplay = document.querySelector("#highest-bid");
bidForm.addEventListener("submit", (event) => {
event.preventDefault();
const newBidAmount = parseInt(bidInput.value);
if (isNaN(newBidAmount) || newBidAmount <= currentHighestBid)
{
alert("Invalid bid amount. Please enter a higher bid.");
} else
{
currentHighestBid = newBidAmount;
currentBidDisplay.textContent = `$${currentHighestBid}`;
highestBidDisplay.textContent = `$${currentHighestBid}`;
if (autoBidEnabled)
{
const autoBidAmount = Math.min(autoBidder.maxBid, currentHighestBid + 1);
if (autoBidAmount > currentHighestBid)
{
currentHighestBid = autoBidAmount;
currentBidDisplay.textContent = `$${currentHighestBid}`;
highestBidDisplay.textContent = `$${currentHighestBid}`;
alert(`${autoBidder.name} auto-bid $${autoBidAmount}.`);
}
}
}
}
);
const autoBidForm = document.querySelector("#auto-bid-form");
const autoBidderNameInput = document.querySelector("#bidder-name");
const maxBidAmountInput = document.querySelector("#max-bid-amount");
let autoBidder = null;
let autoBidEnabled = false;
autoBidForm.addEventListener("submit", (event) => {
event.preventDefault();
const bidderName = autoBidderNameInput.value;
const maxBidAmount = parseInt(maxBidAmountInput.value);
if (isNaN(maxBidAmount) || maxBidAmount <= currentHighestBid)
{
alert("Invalid max bid amount. Please enter a higher max bid.");
} else
{
autoBidder = { name: bidderName, maxBid: maxBidAmount };
autoBidEnabled = true;
alert(`Auto-bidding enabled for ${bidderName} with a max bid of $${maxBidAmount}.`);
}
});
const timeDisplay = document.querySelector("#time-remaining");
let timeRemaining = 60;
const countdownInterval = setInterval(() => {
timeRemaining--;
timeDisplay.textContent = `Time Remaining: ${timeRemaining} seconds`;
if (timeRemaining <= 0)
{
clearInterval(countdownInterval);
bidInput.disabled = true;
const winnerMessage = `Auction ended. The winner is John with a bid of $${currentHighestBid}.`;
alert(winnerMessage);
}
}, 1000);
setInterval(() => {
highestBidDisplay.textContent = `$${currentHighestBid}`;
}, 1000);