From 046dfdf857d174f8231133c58f97394e742c1f06 Mon Sep 17 00:00:00 2001 From: Mushirul Hassan Date: Fri, 8 Nov 2024 00:11:31 +0530 Subject: [PATCH] Add cart.js --- cart.js | 72 ++++++++++++++++----------------------------------------- 1 file changed, 20 insertions(+), 52 deletions(-) diff --git a/cart.js b/cart.js index 451d2061..585595ec 100644 --- a/cart.js +++ b/cart.js @@ -7,7 +7,7 @@ function addItemToCart() { const addToCart = function(name, price){ let cartItems = localStorage.getItem('cartItems'); cartItems = cartItems ? JSON.parse(cartItems) : []; - if(name==null && price==null) return; + if(name == null || price == null) return; const existingItem = cartItems.find(item => item.name === name); if (!existingItem) { cartItems.push({ name, price }); @@ -17,7 +17,6 @@ const addToCart = function(name, price){ updateCartDisplay(); calculateBill(); - } const updateCartDisplay = function() { @@ -39,63 +38,32 @@ const updateCartDisplay = function() { }); } - -// calculate total bill amount let total = 0; -const calculateBill = ()=>{ - itemPrices = document.querySelectorAll(".price"); - for (p of itemPrices){ - if (p!=null){ - console.log(p.innerText); - total += parseFloat(p.innerText.replace('$','')); +const calculateBill = () => { + total = 0; // Reset total for each calculation + const itemPrices = document.querySelectorAll(".price"); + itemPrices.forEach(p => { + if (p) { + total += parseFloat(p.innerText.replace('$', '')); } - } + }); - console.log(total); - if(total!=0 && !isNaN(total)){ - document.getElementById("bill").innerText = "$" + total.toFixed(2) + if (total !== 0 && !isNaN(total)) { + document.getElementById("bill").innerText = "$" + total.toFixed(2); } - } document.addEventListener('DOMContentLoaded', function () { addItemToCart(); + updateCartDisplay(); // Update the display on page load + calculateBill(); // Calculate the total on page load }); -let orderBtn = document.querySelector(".butt"); -orderBtn.addEventListener("click", ()=>{ - if(total==0){ - alert("Please add something in the cart to place the order"); - } - else{ - - alert("Order placed!"); - } -}) - -// Prioritizing Image Loading - - +const orderBtn = document.querySelector(".butt"); +orderBtn.addEventListener("click", () => { + if (total === 0) { + alert("Please add something in the cart to place the order"); + } else { + alert("Order placed!"); + } +});