أولاً، نحتاج إلى تسجيل ما يرسله المستخدم في النموذج. سنستخدم جافا سكريبت لمنع تحديث الصفحة عند إرسال النموذج، مما يسمح لنا بمعالجة الحساب دون فقدان الإدخال.
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); });
الآن، لنقم بإنشاء الوظيفة التي تتحقق من عدد الأشخاص الذين يمكنهم الإقامة في كل نوع من أنواع الغرف. هذه الوظيفة مهمة لأن غرف الفندق لديها حدود لعدد الضيوف الذين يمكنها استيعابهم.
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 } }
بعد ذلك، نقوم بحساب سعر الغرفة لليلة بناءً على تاريخ تسجيل الوصول ونوع الغرفة. تكون المعدلات أعلى خلال موسم الذروة (من يونيو إلى أغسطس) وتنخفض خلال بقية العام.
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 && monthماذا يفعل:
أخيرًا، نقوم بحساب التكلفة الإجمالية للإقامة. نحن نطبق أي خصومات ثم نضيف الضريبة للحصول على الإجمالي النهائي.
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)}
`; }
وهذا كل شيء! لقد قمنا ببناء مُقدِّر تكلفة الغرفة باستخدام JavaScript بشكل بسيط وعملي.
يتوفر هنا My GitHub Repo لهذا الرمز! بما في ذلك تصميم HTML وBootstrap الأساسي للصفحة
ثوني كيو
مطور ومصمم إبداعي
طالب تطوير الواجهة الأمامية | سنة حتى المتحدة
تنصل: جميع الموارد المقدمة هي جزئيًا من الإنترنت. إذا كان هناك أي انتهاك لحقوق الطبع والنشر الخاصة بك أو الحقوق والمصالح الأخرى، فيرجى توضيح الأسباب التفصيلية وتقديم دليل على حقوق الطبع والنشر أو الحقوق والمصالح ثم إرسالها إلى البريد الإلكتروني: [email protected]. سوف نتعامل مع الأمر لك في أقرب وقت ممكن.
Copyright© 2022 湘ICP备2022001581号-3