Skip to content

Commit

Permalink
Merge pull request #67 from satyam1024/main
Browse files Browse the repository at this point in the history
Duplicate Items in Cart #52
  • Loading branch information
Altair-05 authored Oct 2, 2024
2 parents 269e524 + 7b52318 commit 64ff968
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
20 changes: 16 additions & 4 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,18 @@ let cart = [];

// Function to add items to cart
function addToCart(productName, price) {
cart.push({ name: productName, price: price });

let existingCartItem = cart.find(item => item?.name == productName);
if(existingCartItem){
existingCartItem.price= existingCartItem.price + price;
existingCartItem.No= existingCartItem.No +1

}
else{
cart.push({ name: productName, No: 1,price: price});
}
displayCart();

}

// Function to remove items from cart
Expand All @@ -54,7 +64,7 @@ function displayCart() {
let total = 0;

cart.forEach((item, index) => {
cartItemsHTML += `<li>${item.name} - $${item.price.toFixed(2)} <button onclick="removeFromCart(${index})">Remove</button></li>`;
cartItemsHTML += `<li>${item.name} ( ${item.No} ) - $${item.price.toFixed(2)} <button onclick="removeFromCart(${index})">Remove</button></li>`;
total += item.price;
});

Expand All @@ -63,6 +73,7 @@ function displayCart() {
displayOrder();
}


// Function to simulate checkout
function checkout() {
if (cart.length === 0) {
Expand All @@ -87,7 +98,7 @@ function displayOrder() {

let orderHTML = '<h3>Current Order:</h3><ul>';
cart.forEach((item, index) => {
orderHTML += `<li>${item.name} - $${item.price.toFixed(2)}</li>`;
orderHTML += `<li>${item.name} ( ${item.No} )- $${item.price.toFixed(2)}</li>`;
});
orderHTML += '</ul>';

Expand Down Expand Up @@ -119,4 +130,5 @@ function editOrder() {
alert('Your order has been cleared. You can now add new items.');
}
}
displayOrder();
displayOrder();

4 changes: 3 additions & 1 deletion style.css
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ button:hover {
.header .cart-items-container .cart {
margin: 1rem 0;
display: flex;
flex-direction: column;
align-items: center;

}

.header .cart-items-container .cart-item .fa-times {
Expand Down Expand Up @@ -605,4 +607,4 @@ footer .credit {
html {
font-size: 50%;
}
}
}

0 comments on commit 64ff968

Please sign in to comment.