diff --git a/index.html b/index.html index 752a993..4803c07 100644 --- a/index.html +++ b/index.html @@ -17,8 +17,8 @@
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 = `