First, we need to capture what the user submits in the form. We'll use JavaScript to prevent the page from refreshing when the form is submitted, allowing us to handle the calculation without losing the input.
document.getElementById("costForm").addEventListener("submit", function (event) { event.preventDefault(); // Prevents the page from reloading // 1. Get the values from the form const name = document.getElementById("name").value; const checkInDate = document.getElementById("checkInDate").value; const nights = parseInt(document.getElementById("nights").value); const roomType = document.querySelector('input[name="roomType"]:checked').value; const discount = document.querySelector('input[name="discount"]:checked').value; const adults = parseInt(document.getElementById("adults").value); const children = parseInt(document.getElementById("children").value); // 2. Check room occupancy const maxOccupancy = getMaxOccupancy(roomType); if (adults children > maxOccupancy) { document.getElementById("messageDiv").innerText = `The room you selected cannot hold your group. Max occupancy is ${maxOccupancy}.`; return; } // 3. Clear previous messages and calculate the cost document.getElementById("messageDiv").innerText = ""; calculateCost(roomType, checkInDate, nights, discount); });
Now, let's create the function that checks how many people can stay in each room type. This function is important because the hotel rooms have limits on how many guests they can accommodate.
function getMaxOccupancy(roomType) { if (roomType === "queen") { return 5; // Queen rooms can hold up to 5 people } else if (roomType === "king") { return 2; // King rooms can hold up to 2 people } else if (roomType === "suite") { return 6; // 2-Bedroom Suites can hold up to 6 people } }
Next, we calculate the nightly room rate based on the check-in date and room type. The rates are higher during the high season (June to August) and lower during the rest of the year.
function getRoomRate(checkInDate, roomType) { const date = new Date(checkInDate); // Convert the check-in date to a Date object const month = date.getMonth() 1; // Get the month (months are 0-indexed) let rate; // Variable to store the room rate // High season (June to August) rates are more expensive if (month >= 6 && monthWHAT IT DOES:
Finally, we calculate the total cost of the stay. We apply any discounts and then add tax to get the final total.
function calculateCost(roomType, checkInDate, nights, discount) { // 1. Get the room rate per night const roomRate = getRoomRate(checkInDate, roomType); // 2. Calculate the total room cost before discounts let totalRoomCost = roomRate * nights; // 3. Apply the discount (if any) let discountRate = 0; // Start with no discount if (discount === "aaa") { discountRate = totalRoomCost * 0.1; // 10% discount for AAA/Senior } else if (discount === "military") { discountRate = totalRoomCost * 0.2; // 20% discount for Military } // Subtract the discount from the total room cost const discountedCost = totalRoomCost - discountRate; // 4. Calculate the tax (12% of the discounted total cost) const tax = discountedCost * 0.12; // 5. Calculate the final total cost, including tax const totalCost = discountedCost tax; // 6. Display the results on the page document.getElementById("result").innerHTML = `Room Rate per Night: $${roomRate.toFixed(2)}
Number of Nights: ${nights}
Discount Amount: $${discountRate.toFixed(2)}
Tax: $${tax.toFixed(2)}
Total Cost (with tax): $${totalCost.toFixed(2)}
`; }
And that’s it! We’ve built a simple yet functional Room Cost Estimator using JavaScript.
My GitHub Repo for this code is here! Including the basic HTML and Bootstrap styling for the page
Thounny Keo
Creative Developer & Designer
Frontend Development Student | Year Up United
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3