diff --git a/index.html b/index.html index 752a993..942e0bb 100644 --- a/index.html +++ b/index.html @@ -32,10 +32,22 @@
diff --git a/js/index.js b/js/index.js index 75405d6..caa3822 100644 --- a/js/index.js +++ b/js/index.js @@ -2,22 +2,24 @@ function updateSubtotal(product) { console.log('Calculating subtotal, yey!'); - - //... your code goes here + const price = product.querySelector('.price span'); + const quantity = product.getElementsByTagName('input')[0]; + const sub= parseFloat(price.textContent) * parseInt(quantity.value); + const subtotal= product.querySelector('.subtotal span'); + subtotal.innerHTML = sub; + return sub } 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; + for (let i=0 ; i < products.length ; i++){ + total += updateSubtotal(products[i]); + + } - // ITERATION 3 - //... your code goes here + document.querySelector('#total-value span').innerHTML = total; } // ITERATION 4 @@ -25,18 +27,55 @@ function calculateAll() { function removeProduct(event) { const target = event.currentTarget; console.log('The target in remove is:', target); - //... your code goes here + const productRow = target.parentNode.parentNode; + productRow.parentNode.removeChild(productRow); + 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 = ` +