From bb3c3ab0da22b1fdef6f04021f2d36c55faf3a61 Mon Sep 17 00:00:00 2001 From: FranRevi8 Date: Thu, 20 Jun 2024 19:26:25 +0200 Subject: [PATCH 1/2] done -1 error pending to be solved- --- index.html | 17 ++++++++++-- src/index.js | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 90 insertions(+), 4 deletions(-) diff --git a/index.html b/index.html index 883839837..34261cc77 100644 --- a/index.html +++ b/index.html @@ -33,9 +33,22 @@

Ironhack Cart

+ + + Ironhack Beach Towel + + $12.50 + + + + $0 + + + + - +

diff --git a/src/index.js b/src/index.js index 75405d6cf..c63708f76 100644 --- a/src/index.js +++ b/src/index.js @@ -4,20 +4,46 @@ function updateSubtotal(product) { console.log('Calculating subtotal, yey!'); //... your code goes here + let priceElement = product.querySelector(".price span"); + let quantityElement = product.querySelector(".quantity input"); + + let price = priceElement.innerText; + let quantity = quantityElement.value; + + let subtotalPrice = price * quantity; + + let subtotalElement = product.querySelector(".subtotal span"); + subtotalElement.innerText = subtotalPrice; } 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.querySelectorAll(".product"); + + for (let i = 0; i < products.length; i++) { + updateSubtotal(products[i]); + } + // ITERATION 3 //... your code goes here + + let total = 0 + + for(let i = 0; i + ${newProduct} + + $${newPrice} + + + + $0 + + + ` + + document.querySelector("tbody").appendChild(newProductRow); + + newProductInput.value = ""; + newPriceInput.value = 0 ; + + newProductRow.querySelector(".btn-remove").addEventListener("click", removeProduct); } window.addEventListener('load', () => { @@ -39,4 +104,12 @@ window.addEventListener('load', () => { calculatePricesBtn.addEventListener('click', calculateAll); //... your code goes here + + const removeProductBtn = document.querySelectorAll(".btn-remove"); + removeProductBtn.forEach(element => { + element.addEventListener("click", removeProduct) + }); + + const newProductBtn = document.querySelector("#create"); + newProductBtn.addEventListener("click", createProduct) }); From 6e52666bfaa7f1829380a264d70e3d29776117db Mon Sep 17 00:00:00 2001 From: FranRevi8 Date: Fri, 21 Jun 2024 09:22:04 +0200 Subject: [PATCH 2/2] debugged --- src/index.js | 55 +++++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/src/index.js b/src/index.js index c63708f76..ace149350 100644 --- a/src/index.js +++ b/src/index.js @@ -1,15 +1,15 @@ // ITERATION 1 function updateSubtotal(product) { - console.log('Calculating subtotal, yey!'); + console.log("Calculating subtotal, yey!"); //... your code goes here let priceElement = product.querySelector(".price span"); let quantityElement = product.querySelector(".quantity input"); - + let price = priceElement.innerText; let quantity = quantityElement.value; - + let subtotalPrice = price * quantity; let subtotalElement = product.querySelector(".subtotal span"); @@ -35,34 +35,35 @@ function calculateAll() { // ITERATION 3 //... your code goes here - let total = 0 + let total = 0; - for(let i = 0; i $0 - ` + `; document.querySelector("tbody").appendChild(newProductRow); newProductInput.value = ""; - newPriceInput.value = 0 ; + newPriceInput.value = 0; - newProductRow.querySelector(".btn-remove").addEventListener("click", removeProduct); + newProductRow + .querySelector(".btn-remove") + .addEventListener("click", removeProduct); } -window.addEventListener('load', () => { - const calculatePricesBtn = document.getElementById('calculate'); - calculatePricesBtn.addEventListener('click', calculateAll); +window.addEventListener("load", () => { + const calculatePricesBtn = document.getElementById("calculate"); + calculatePricesBtn.addEventListener("click", calculateAll); //... your code goes here const removeProductBtn = document.querySelectorAll(".btn-remove"); - removeProductBtn.forEach(element => { - element.addEventListener("click", removeProduct) + removeProductBtn.forEach((element) => { + element.addEventListener("click", removeProduct); }); const newProductBtn = document.querySelector("#create"); - newProductBtn.addEventListener("click", createProduct) + newProductBtn.addEventListener("click", createProduct); });