Skip to content

Commit

Permalink
Created a function to calculate the total cost and display it
Browse files Browse the repository at this point in the history
  • Loading branch information
DiksonIvySon committed Oct 18, 2023
1 parent ae73fa5 commit d751bbc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
21 changes: 19 additions & 2 deletions landingPage/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function handleAddToCart(id) {
let imageURL = document.querySelector(imageClass).getAttribute('src');

addToCartList(name, price, imageURL)

}

//function to make a cartItem object and add it to cartList
Expand Down Expand Up @@ -60,7 +61,7 @@ function display_cartList() {

cartItem_container.appendChild(cart_Item);
}
//call setCartNumber()
//call setCartNumber function
setCartNumber();
}

Expand All @@ -81,10 +82,26 @@ function handleQuantityChange(index) {

//rerender the cartList display
display_cartList()

}

//function to change the items number in the cart
//function to change the items number in the cart when item is added
function setCartNumber() {
let items_inCart = cartList.length;
document.querySelector('#cart-number').textContent = items_inCart;

//call totalCost function
totalCost()
}

//function to calculate the total cost and display it
function totalCost() {
let total_cost = 0;

for (i = 0; i < cartList.length; i++) {
total_cost += cartList[i].quantity_price;
}
console.log(total_cost)

document.querySelector('#total-cost').textContent = total_cost;
}
4 changes: 2 additions & 2 deletions landingPage/store.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ <h2>CART</h2>
<div>
<div>
<strong>Items</strong>
<span id="cart-number">4</span>
<span id="cart-number">0</span>
</div>
<div>
<strong>Total</strong>
<span>R1 000</span>
<span>R<span id="total-cost">0</span></span>
</div>
<div>
<button role="button">PURCHASE</button>
Expand Down

0 comments on commit d751bbc

Please sign in to comment.