Permalink
Cannot retrieve contributors at this time
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?
lab7/app.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
62 lines (55 sloc)
2.5 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var auctionItem = { | |
name: "Item Name", | |
description: "Reference 3838/1 Ellipse | A yellow gold bracelet watch, Circa 1985", | |
startingBid: 100, | |
highestBid: 100, | |
}; | |
let currentHighestBid = 100; | |
let currentBids = {}; // Object to store current bids and bidders' maximum bids | |
document.getElementById("name").innerHTML = auctionItem.name; | |
document.getElementById("description").innerHTML = auctionItem.description; | |
document.getElementById("highest-bid").innerHTML = "$" + auctionItem.highestBid; | |
document.getElementById("current-bid").innerHTML = "$" + auctionItem.startingBid; | |
const bidForm = document.querySelector("form"); | |
const bidInput = document.querySelector("#bid-amount"); | |
const maxBidInput = document.querySelector("#max-bid-amount"); // Input for maximum 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); | |
const maxBidAmount = parseInt(maxBidInput.value); // Retrieve maximum bid amount | |
if (isNaN(newBidAmount) || newBidAmount <= currentHighestBid || newBidAmount > maxBidAmount) { | |
alert("Invalid bid amount. Please enter a valid bid within your maximum limit."); | |
} else { | |
currentHighestBid = newBidAmount; | |
currentBidDisplay.textContent = `$${currentHighestBid}`; | |
highestBidDisplay.textContent = `$${currentHighestBid}`; | |
// Update the current bid and maximum bid for the user (assuming 'John') | |
currentBids["John"] = { currentBid: newBidAmount, maxBid: maxBidAmount }; | |
} | |
}); | |
// Automatically increase bid based on maximum bid set by users | |
setInterval(() => { | |
Object.keys(currentBids).forEach(bidderName => { | |
const bidderInfo = currentBids[bidderName]; | |
if (bidderInfo.maxBid > currentHighestBid) { | |
currentHighestBid += 1; // Automatically increase bid by $1 | |
currentBidDisplay.textContent = `$${currentHighestBid}`; | |
highestBidDisplay.textContent = `$${currentHighestBid}`; | |
} | |
}); | |
}, 1000); | |
// Countdown timer | |
const timeDisplay = document.querySelector("#time-remaining"); | |
let timeRemaining = 60; // 60 seconds = 1 minute | |
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); |