Skip to content
Open
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
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ <h1>Root Cart</h1>
<!-- Iteration 2: Add more products here -->
</tbody>
<tfoot>
<!-- <tr class="create-product">
<tr class="create-product">
<td>
<input type="text" placeholder="Product Name" />
</td>
Expand All @@ -47,7 +47,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: 49 additions & 18 deletions js/index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,73 @@
// ITERATION 1

function updateSubtotal(product) {
console.log('Calculating subtotal, yey!');

//... your code goes here
const price = parseFloat(product.querySelector('.price span').textContent);
const quantity = parseInt(product.querySelector('.quantity input').value);
const subtotal = price * quantity;
product.querySelector('.subtotal span').textContent = subtotal.toFixed(2);
return subtotal;
}

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
function calculateAll() {
const products = document.querySelectorAll('.product');
let total = 0;

products.forEach(product => {
total += updateSubtotal(product);
});

// ITERATION 3
//... your code goes here
document.querySelector('#total-value span').textContent = total.toFixed(2);
}


// ITERATION 4

function removeProduct(event) {
const target = event.currentTarget;
console.log('The target in remove is:', target);
//... your code goes here
const productRow = event.currentTarget.closest('.product');
productRow.remove();
calculateAll();
}

document.querySelectorAll('.btn-remove').forEach(button => {
button.addEventListener('click', removeProduct);
});



// ITERATION 5

function createProduct() {
//... your code goes here
const name = document.querySelector('.create-product input[type="text"]').value;
const price = parseFloat(document.querySelector('.create-product input[type="number"]').value).toFixed(2);

if (!name || price <= 0) return; // Ensure valid input

const table = document.querySelector('#cart tbody');
const newRow = document.createElement('tr');
newRow.classList.add('product');

newRow.innerHTML = `
<td class="name"><span>${name}</span></td>
<td class="price">$<span>${price}</span></td>
<td class="quantity"><input type="number" value="0" min="0" /></td>
<td class="subtotal">$<span>0</span></td>
<td class="action"><button class="btn btn-remove">Remove</button></td>
`;

table.appendChild(newRow);
newRow.querySelector('.btn-remove').addEventListener('click', removeProduct);
}
document.getElementById('create').addEventListener('click', createProduct);


window.addEventListener('load', () => {
const calculatePricesBtn = document.getElementById('calculate');
calculatePricesBtn.addEventListener('click', calculateAll);

//... your code goes here
document.querySelectorAll('.btn-remove').forEach((btn) => {
btn.addEventListener('click', removeProduct);
});

document.getElementById('create').addEventListener('click', createProduct);
});