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
19 changes: 16 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,30 @@ <h1>Root Cart</h1>
</td>
<td class="price">$<span>25.00</span></td>
<td class="quantity">
<input type="number" value="0" min="0" placeholder="Quantity" />
<input type="number" value="3" min="0" placeholder="Quantity" />
</td>
<td class="subtotal">$<span>0</span></td>
<td class="action">
<button class="btn btn-remove">Remove</button>
</td>
</tr>
<!-- Iteration 2: Add more products here -->
<tr class="product">
<td class="name">
<span>Root Rubber Duck</span>
</td>
<td class="price">$<span>35.00</span></td>
<td class="quantity">
<input type="number" value="2" 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 +60,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
63 changes: 61 additions & 2 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,32 @@ function updateSubtotal(product) {
console.log('Calculating subtotal, yey!');

//... your code goes here
const price = product.querySelector('.price span').innerText;
const quantity = product.querySelector('.quantity input').value;
const subtotal = price * quantity;
product.querySelector('.subtotal span').innerText = subtotal;
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);
/* const singleProduct = document.querySelector('.product');
updateSubtotal(singleProduct); */
// end of test

// ITERATION 2
//... your code goes here
const products = document.getElementsByClassName('product');

// ITERATION 3
//... your code goes here
let total = 0;
for (let i = 0; i < products.length; i++) {
total += updateSubtotal(products[i]);
}
document.querySelector('#total-value span').innerText = total;

}

// ITERATION 4
Expand All @@ -26,17 +38,64 @@ function removeProduct(event) {
const target = event.currentTarget;
console.log('The target in remove is:', target);
//... your code goes here
const productRow = target.closest('.product');
productRow.remove();
calculateAll();
}

// ITERATION 5

function createProduct() {
//... your code goes here
const createRow = document.querySelector('.create-product');
const nameInput = createRow.querySelector('input[type="text"]');
const priceInput = createRow.querySelector('input[type="number"]');

const name = nameInput.value;
const price = parseFloat(priceInput.value).toFixed(2);

if (name === '' || isNaN(price) || price <= 0) {
alert('Please enter a valid product name and price.');
return;
}

const tableBody = 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" placeholder="Quantity" />
</td>
<td class="subtotal">$<span>0</span></td>
<td class="action">
<button class="btn btn-remove">Remove</button>
</td>
`;

tableBody.appendChild(newRow);

// Clear input fields
nameInput.value = '';
priceInput.value = '';

// Add event listener to the new remove button
const removeBtn = newRow.querySelector('.btn-remove');
removeBtn.addEventListener('click', removeProduct);
}

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

//... your code goes here
const removeButtons = document.getElementsByClassName('btn-remove');
for (let i = 0; i < removeButtons.length; i++) {
removeButtons[i].addEventListener('click', removeProduct);
}

const createButton = document.getElementById('create');
});