From 5f30535c37769df5f9d6d425dfefa49daec131d2 Mon Sep 17 00:00:00 2001 From: Aazim Rafeekh Date: Sun, 6 Oct 2024 22:15:21 +0530 Subject: [PATCH] done --- index.html | 18 +++++++++++--- js/index.js | 67 ++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 68 insertions(+), 17 deletions(-) diff --git a/index.html b/index.html index 752a993..942e0bb 100644 --- a/index.html +++ b/index.html @@ -32,10 +32,22 @@

Root Cart

- + + + Root Rubber Cat + + $45.00 + + + + $0 + + + + - +

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 = ` + ${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); + } + + });