diff --git a/index.html b/index.html index 752a993..feb2c3e 100644 --- a/index.html +++ b/index.html @@ -25,7 +25,7 @@

Root Cart

$25.00 - + $0 @@ -33,9 +33,22 @@

Root Cart

+ + + Root Rubber Duck + + $35.00 + + + + $0 + + + + - +

diff --git a/js/index.js b/js/index.js index 75405d6..8f4bbb2 100644 --- a/js/index.js +++ b/js/index.js @@ -4,20 +4,32 @@ function updateSubtotal(product) { console.log('Calculating subtotal, yey!'); //... your code goes here + const price = product.querySelector('.price span').innerText; + const quantity = product.querySelector('.quantity input').value; + const subtotal = price * quantity; + product.querySelector('.subtotal span').innerText = subtotal; + return subtotal; } 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); + /* const singleProduct = document.querySelector('.product'); + updateSubtotal(singleProduct); */ // end of test // ITERATION 2 //... your code goes here + const products = document.getElementsByClassName('product'); // ITERATION 3 //... your code goes here + let total = 0; + for (let i = 0; i < products.length; i++) { + total += updateSubtotal(products[i]); + } + document.querySelector('#total-value span').innerText = total; + } // ITERATION 4 @@ -26,12 +38,53 @@ 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 createRow = document.querySelector('.create-product'); + const nameInput = createRow.querySelector('input[type="text"]'); + const priceInput = createRow.querySelector('input[type="number"]'); + + const name = nameInput.value; + const price = parseFloat(priceInput.value).toFixed(2); + + if (name === '' || isNaN(price) || price <= 0) { + alert('Please enter a valid product name and price.'); + return; + } + + const tableBody = document.querySelector('#cart tbody'); + const newRow = document.createElement('tr'); + newRow.classList.add('product'); + newRow.innerHTML = ` + + ${name} + + $${price} + + + + $0 + + + + `; + + tableBody.appendChild(newRow); + + // Clear input fields + nameInput.value = ''; + priceInput.value = ''; + + // Add event listener to the new remove button + const removeBtn = newRow.querySelector('.btn-remove'); + removeBtn.addEventListener('click', removeProduct); } window.addEventListener('load', () => { @@ -39,4 +92,10 @@ window.addEventListener('load', () => { calculatePricesBtn.addEventListener('click', calculateAll); //... your code goes here + const removeButtons = document.getElementsByClassName('btn-remove'); + for (let i = 0; i < removeButtons.length; i++) { + removeButtons[i].addEventListener('click', removeProduct); + } + + const createButton = document.getElementById('create'); });