Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

culminado #3238

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -578,4 +578,4 @@ If the link shown is the same as the main Ironhack repository, you will need to

[Back to top](#faqs)

</details>
</details>
42 changes: 28 additions & 14 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="en">
<html lang="es">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
Expand All @@ -11,47 +11,61 @@ <h1>Ironhack Cart</h1>
<table id="cart">
<thead>
<tr>
<th>Product Name</th>
<th>Unit Price</th>
<th>Quantity</th>
<th>Nombre del producto</th>
<th>Precio unitario</th>
<th>Cantidad</th>
<th>Subtotal</th>
<th>Action</th>
<th>Medidas</th>
</tr>
</thead>
<tbody>
<tr class="product">
<td class="name">
<span>Ironhack Rubber Duck</span>
<span>Pato de goma de Ironhack</span>
</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="0" min="0" placeholder="Cantidad" />
</td>
<td class="subtotal">$<span>0</span></td>
<td class="action">
<button class="btn btn-remove">Remove</button>
<button class="btn btn-remove">Retirar</button>
</td>
</tr>
<tr class="product">
<td class="name">
<span>Gato</span>
</td>
<td class="price">$<span>15.00</span></td>
<td class="quantity">
<input type="number" value="0" min="0" placeholder="Cantidad" />
</td>
<td class="subtotal">$<span>0</span></td>
<td class="action">
<button class="btn btn-remove">Retirar</button>
</td>
</tr>

<!-- Iteration 2: Add more products here -->
</tbody>
<tfoot>
<!-- <tr class="create-product">
<tr class="create-product">
<td>
<input type="text" placeholder="Product Name" />
<input type="text" placeholder="Nombre del producto" />
</td>
<td>
<input type="number" min="0" value="0" placeholder="Product Price" />
<input type="number" min="0" value="0" placeholder="Precio unitario" />
</td>
<td></td>
<td></td>
<td>
<button id="create" class="btn">Create Product</button>
<button id="create" class="btn">Crear producto</button>
</td>
</tr> -->
</tr>
</tfoot>
</table>
<p class="calculate-total">
<button id="calculate" class="btn btn-success">Calculate Prices</button>
<button id="calculate" class="btn btn-success">Calcular precios</button>
</p>
<h2 id="total-value">Total: $<span>0</span></h2>
<script type="text/javascript" src="./src/index.js"></script>
Expand Down
64 changes: 49 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,74 @@
function updateSubtotal(product) {
console.log('Calculating subtotal, yey!');

//... your code goes here
const price = parseFloat(product.querySelector('.price span').innerText.replace('$', ''));
const quantity = parseInt(product.querySelector('.quantity input').value);

const subtotal = price * quantity;
product.querySelector('.subtotal span').innerText = 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
const allProducts = document.querySelectorAll('.product');
let total = 0;

// ITERATION 2
//... your code goes here
allProducts.forEach(product => {
total += updateSubtotal(product);
});

// ITERATION 3
//... your code goes here
document.querySelector('#total-value span').innerText = 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 = target.closest('.product');
productRow.remove();

calculateAll();
}
// ITERATION 5

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

const newProduct = document.createElement('tr');
newProduct.classList.add('product');
newProduct.innerHTML = `
<td class="name">
<span>${productName}</span>
</td>
<td class="price">$<span>${parseFloat(productPrice).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(newProduct);

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

window.addEventListener('load', () => {
const createProductBtn = document.getElementById('create');
createProductBtn.addEventListener('click', createProduct);

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