From 0bdcd5e23c730856cab9b444e0c3183f86bcb5dd Mon Sep 17 00:00:00 2001 From: SiyaM108 Date: Sun, 28 Sep 2025 12:23:55 +0100 Subject: [PATCH 1/4] complete updateSubtotal() --- js/index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/js/index.js b/js/index.js index 75405d6..cf0726c 100644 --- a/js/index.js +++ b/js/index.js @@ -4,6 +4,11 @@ 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() { From ed6ef16934628d62471e5a159bafb16d47e63b31 Mon Sep 17 00:00:00 2001 From: SiyaM108 Date: Sun, 28 Sep 2025 12:50:16 +0100 Subject: [PATCH 2/4] complete calculateAll() and make duplicate of tr with class product --- index.html | 15 ++++++++++++++- js/index.js | 10 ++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/index.html b/index.html index 752a993..6b7226b 100644 --- a/index.html +++ b/index.html @@ -25,7 +25,7 @@

Root Cart

$25.00 - + $0 @@ -33,6 +33,19 @@

Root Cart

+ + + Root Rubber Duck + + $25.00 + + + + $0 + + + + +

diff --git a/js/index.js b/js/index.js index e095c70..8f4bbb2 100644 --- a/js/index.js +++ b/js/index.js @@ -47,6 +47,44 @@ function removeProduct(event) { 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', () => { @@ -54,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'); });