From b176a0b0ed90f98efbcd0cd24ba418753d6651a5 Mon Sep 17 00:00:00 2001 From: Yerko Arce Date: Sun, 19 Jan 2025 14:49:01 -0300 Subject: [PATCH] Done! --- index.html | 25 ++++++++++++++----- js/index.js | 72 ++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 76 insertions(+), 21 deletions(-) diff --git a/index.html b/index.html index 752a993..4803c07 100644 --- a/index.html +++ b/index.html @@ -17,8 +17,8 @@

Root Cart

Subtotal Action - - + + Root Rubber Duck @@ -32,22 +32,35 @@

Root Cart

+ + + PC + + $425.00 + + + + $0 + + + + - +

diff --git a/js/index.js b/js/index.js index 75405d6..a292a49 100644 --- a/js/index.js +++ b/js/index.js @@ -1,42 +1,84 @@ // ITERATION 1 function updateSubtotal(product) { - console.log('Calculating subtotal, yey!'); + const price = parseFloat(product.querySelector('.price span').textContent) + const quantity = parseInt(product.querySelector('.quantity input').value) + + const total = price * quantity + const subtotal = product.querySelector('.subtotal span') + subtotal.textContent = total + return total - //... your code goes here } function calculateAll() { - // code in the following two lines is added just for testing purposes. - // it runs when only iteration 1 is completed. at later point, it can be removed. - const singleProduct = document.querySelector('.product'); - updateSubtotal(singleProduct); - // end of test - // ITERATION 2 - //... your code goes here - + const products = document.getElementsByClassName('product'); + let total = 0 + Array.from(products).forEach(product => { + updateSubtotal(product) + total += updateSubtotal(product) + }); + // ITERATION 3 - //... your code goes here + let totalValue = document.querySelector('#total-value span') + totalValue.textContent = total } // ITERATION 4 function removeProduct(event) { const target = event.currentTarget; - console.log('The target in remove is:', target); - //... your code goes here + const product = target.closest('.product') + // Total to discount + const price = parseFloat(product.querySelector('.price span').textContent) + const quantity = parseInt(product.querySelector('.quantity input').value) + const total = price * quantity + // Discount + const totalValue = document.querySelector('#total-value span') + totalValue.textContent -= total + // Remove + target.closest('.product').remove() } // ITERATION 5 function createProduct() { - //... your code goes here + const newProduct = document.querySelector('.new-product').value + const newProductPrice = parseFloat(document.querySelector('.new-product-quantity').value) + // Adding product + const cart = document.querySelector('#cart-items') + const newProductElement = document.createElement('tr') + newProductElement.classList.add('product') + newProductElement.innerHTML = ` + ${newProduct} + + $${newProductPrice}.00 + + + + $0 + + + ` + cart.appendChild(newProductElement) + + const removeButton = newProductElement.querySelector('.btn-remove') + removeButton.addEventListener('click', removeProduct) + + document.querySelector('.new-product').value = '' + document.querySelector('.new-product-quantity').value = 0 } window.addEventListener('load', () => { const calculatePricesBtn = document.getElementById('calculate'); calculatePricesBtn.addEventListener('click', calculateAll); - //... your code goes here + const removeProductBtn = document.querySelectorAll('.btn-remove') + Array.from(removeProductBtn).forEach(btn => { + btn.addEventListener('click', removeProduct) + }) + + const createProductBtn = document.querySelector('#create') + createProductBtn.addEventListener('click', createProduct) });