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.
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); });
Finally, we calculate the total cost of the stay. We apply any discounts and then add tax to get the final total.
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 } }
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
The above is the detailed content of Building a Room Cost Estimator with JavaScript. For more information, please follow other related articles on the PHP Chinese website!