Skip to content
Open

done #82

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,22 @@ <h1>Root Cart</h1>
<button class="btn btn-remove">Remove</button>
</td>
</tr>
<!-- Iteration 2: Add more products here -->
<tr class="product">
<td class="name">
<span>Root Rubber Cat</span>
</td>
<td class="price">$<span>45.00</span></td>
<td class="quantity">
<input type="number" value="0" min="0" placeholder="Quantity" />
</td>
<td class="subtotal">$<span>0</span></td>
<td class="action">
<button class="btn btn-remove">Remove</button>
</td>
</tr>
</tbody>
<tfoot>
<!-- <tr class="create-product">
<tr class="create-product">
<td>
<input type="text" placeholder="Product Name" />
</td>
Expand All @@ -47,7 +59,7 @@ <h1>Root Cart</h1>
<td>
<button id="create" class="btn">Create Product</button>
</td>
</tr> -->
</tr>
</tfoot>
</table>
<p class="calculate-total">
Expand Down
67 changes: 53 additions & 14 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,80 @@

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

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 = `
<td class="name"><span>${name}</span></td>
<td class="price">$<span>${price.toFixed(2)}</span></td>
<td class="quantity"><input type="number" value="0" min="0" placeholder="Quantity" /></td>
<td class="subtotal">$<span>0</span></td>
<td class="action"><button class="btn btn-remove">Remove</button></td>
`;

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);
}


});