diff --git a/index.html b/index.html index 752a993..76eb162 100644 --- a/index.html +++ b/index.html @@ -35,19 +35,21 @@

Root Cart

- +

diff --git a/js/index.js b/js/index.js index 75405d6..ad45086 100644 --- a/js/index.js +++ b/js/index.js @@ -4,7 +4,34 @@ function updateSubtotal(product) { console.log('Calculating subtotal, yey!'); //... your code goes here + if (!product) { + console.error('Product is null or undefined'); + return; + } + + const priceElement = product.querySelector('.price span'); + const quantityElement = product.querySelector('.quantity input'); + + if (!priceElement || !quantityElement) { + console.error('Price or Quantity element not found'); + return; + } + + console.log('Price Element:', priceElement); + console.log('Quantity Element:', quantityElement); + + const price = parseFloat(priceElement.innerText); + const quantity = quantityElement.valueAsNumber; + + console.log('Price:', price, 'Quantity:', quantity); + + const subtotal = price * quantity; + product.querySelector('.subtotal span').innerText = subtotal.toFixed(2); + + return subtotal; } + + function calculateAll() { // code in the following two lines is added just for testing purposes. @@ -15,9 +42,17 @@ function calculateAll() { // ITERATION 2 //... your code goes here + const allProducts = document.querySelectorAll('.product'); + let total = 0; + allProducts.forEach(product => { + total += updateSubtotal(product); + }); + // ITERATION 3 //... your code goes here + document.querySelector('#total-value span').innerText = total.toFixed(2); + } // ITERATION 4 @@ -26,17 +61,62 @@ function removeProduct(event) { const target = event.currentTarget; console.log('The target in remove is:', target); //... your code goes here + const productRow = target.closest('.product'); + productRow.remove(); + calculateAll(); + } // ITERATION 5 function createProduct() { //... your code goes here + const nameInput = document.querySelector('.create-product input[type="text"]'); + const priceInput = document.querySelector('.create-product input[type="number"]'); + + const name = nameInput.value.trim(); + const price = parseFloat(priceInput.value); + + if (name && price > 0) { + const newRow = document.createElement('tr'); + newRow.classList.add('product'); + newRow.innerHTML = ` + ${name} + $${price.toFixed(2)} + + $0 + + `; + + document.querySelector('#cart tbody').appendChild(newRow); + + + newRow.querySelector('.btn-remove').addEventListener('click', removeProduct); + + + nameInput.value = ''; + priceInput.value = 0; + } + } + + + window.addEventListener('load', () => { const calculatePricesBtn = document.getElementById('calculate'); calculatePricesBtn.addEventListener('click', calculateAll); //... your code goes here + const removeButtons = document.querySelectorAll('.btn-remove'); + removeButtons.forEach(button => { + button.addEventListener('click', removeProduct); + }); + + + const createBtn = document.getElementById('create'); + if (createBtn) { + createBtn.addEventListener('click', createProduct); + } }); +