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

resuelto #3234

Open
wants to merge 1 commit 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
5 changes: 5 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ body {
color: #2d354c;
}

a{
text-decoration: none;
color: white;
}

button,
input {
font: inherit;
Expand Down
5 changes: 3 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ <h1>Ironhack 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,11 +47,12 @@ <h1>Ironhack Cart</h1>
<td>
<button id="create" class="btn">Create Product</button>
</td>
</tr> -->
</tr>
</tfoot>
</table>
<p class="calculate-total">
<button id="calculate" class="btn btn-success">Calculate Prices</button>
<button class="btn btn-success"><a href="index.html">Recargar pagina</a></button>
</p>
<h2 id="total-value">Total: $<span>0</span></h2>
<script type="text/javascript" src="./src/index.js"></script>
Expand Down
70 changes: 61 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,92 @@
function updateSubtotal(product) {
console.log('Calculating subtotal, yey!');

//... your code goes here
const price = product.querySelector('.price span');
const quantity = product.querySelector('.quantity input');

const priceValue = parseFloat(price.textContent);
const quantityValue = parseInt(quantity.value);

const subtotalValue = priceValue * quantityValue;

const subtotal = product.querySelector('.subtotal span');
subtotal.textContent = subtotalValue.toFixed(2);

return subtotalValue;
}

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 products = document.getElementsByClassName('product');
let total = 0;

// ITERATION 2
//... your code goes here
for (let product of products) {
total += updateSubtotal(product);
}

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

calculateAll();
}

// ITERATION 5

function createProduct() {
//... your code goes here
const nameInput = document.querySelector('.create-product input[placeholder="Product Name"]');
const priceInput = document.querySelector('.create-product input[placeholder="Product Price"]');

const nameValue = nameInput.value;
const priceValue = parseFloat(priceInput.value).toFixed(2);

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

const cartBody = document.querySelector('#cart tbody');
cartBody.appendChild(newProductRow);

const removeButton = newProductRow.querySelector('.btn-remove');
removeButton.addEventListener('click', removeProduct);

nameInput.value = '';
priceInput.value = '';
}

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 createProductBtn = document.getElementById('create');
createProductBtn.addEventListener('click', createProduct);
});