From a34a16a18fdd52ad6f25df16c44d5065726b9c6b Mon Sep 17 00:00:00 2001 From: Ryson-Theo <148685082+Ryson-Theo@users.noreply.github.com> Date: Tue, 23 Jun 2026 20:32:45 +0530 Subject: [PATCH] Solved lab --- index.html | 20 +++++++++++--- js/index.js | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 88 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index 752a993..98483b6 100644 --- a/index.html +++ b/index.html @@ -32,10 +32,22 @@

Root Cart

- + + + Root Hoodie + + $45.00 + + + + $0 + + + + - +

@@ -56,4 +68,4 @@

Root Cart

Total: $0

- + \ No newline at end of file diff --git a/js/index.js b/js/index.js index 75405d6..277b356 100644 --- a/js/index.js +++ b/js/index.js @@ -4,20 +4,39 @@ function updateSubtotal(product) { console.log('Calculating subtotal, yey!'); //... your code goes here + const priceElement = product.querySelector('.price span'); + const quantityElement = product.querySelector('.quantity input'); + + const priceValue = parseFloat(priceElement.innerText); + const quantityValue = parseInt(quantityElement.value) || 0; + + const subtotalValue = priceValue * quantityValue; + + const subtotalElement = product.querySelector('.subtotal span'); + subtotalElement.innerText = subtotalValue.toFixed(2); + + return subtotalValue; } 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 allProducts = document.getElementsByClassName('product'); + let totalSum = 0; + + for (let product of allProducts) { + totalSum += updateSubtotal(product); + } // ITERATION 3 //... your code goes here + const totalElement = document.querySelector('#total-value span'); + totalElement.innerText = totalSum.toFixed(2); } // ITERATION 4 @@ -26,12 +45,53 @@ 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 createProductRow = document.querySelector('.create-product'); + const nameInput = createProductRow.querySelector('input[type="text"]'); + const priceInput = createProductRow.querySelector('input[type="number"]'); + + const productName = nameInput.value; + const productPrice = parseFloat(priceInput.value) || 0; + + if (!productName || productPrice <= 0) { + alert('Please enter a valid product name and price.'); + return; + } + + const tbody = document.querySelector('#cart tbody'); + const newRow = document.createElement('tr'); + newRow.className = 'product'; + + newRow.innerHTML = ` + + ${productName} + + $${productPrice.toFixed(2)} + + + + $0.00 + + + + `; + + tbody.appendChild(newRow); + + const newRemoveBtn = newRow.querySelector('.btn-remove'); + newRemoveBtn.addEventListener('click', removeProduct); + + nameInput.value = ''; + priceInput.value = 0; } window.addEventListener('load', () => { @@ -39,4 +99,13 @@ window.addEventListener('load', () => { calculatePricesBtn.addEventListener('click', calculateAll); //... your code goes here -}); + const removeButtons = document.querySelectorAll('.btn-remove'); + removeButtons.forEach(button => { + button.addEventListener('click', removeProduct); + }); + + const createButton = document.getElementById('create'); + if (createButton) { + createButton.addEventListener('click', createProduct); + } +}); \ No newline at end of file