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

Solved lab Valeria #3245

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
21 changes: 17 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,22 @@ <h1>Ironhack Cart</h1>
</td>
</tr>
<!-- Iteration 2: Add more products here -->
<tr class="product">
<td class="name">
<span>Ironhack Beach Towel</span>
</td>
<td class="price">$<span>12.50</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">
<!-- <tfoot>
<tr class="create-product">
<td>
<input type="text" placeholder="Product Name" />
</td>
Expand All @@ -47,8 +60,8 @@ <h1>Ironhack Cart</h1>
<td>
<button id="create" class="btn">Create Product</button>
</td>
</tr> -->
</tfoot>
</tr>
</tfoot> -->
</table>
<p class="calculate-total">
<button id="calculate" class="btn btn-success">Calculate Prices</button>
Expand Down
38 changes: 35 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,55 @@
// ITERATION 1

function updateSubtotal(product) {
console.log('Calculating subtotal, yey!');
// console.log('Calculating subtotal, yey!');
const price = product.querySelector('.price span');
const quantity = product.querySelector('.quantity input');

//... your code goes here

const valueOfPrice = Number(price.innerHTML);
console.log(valueOfPrice);

const valueOfQuantity = Number(quantity.value);
console.log(valueOfQuantity);


let subtotalPrice = 0;
if (valueOfPrice === valueOfPrice && valueOfQuantity === valueOfQuantity) {
subtotalPrice = valueOfPrice * valueOfQuantity;
}
else {
return 0;
}
const subtotalElement = product.querySelector('.subtotal span');
subtotalElement.innerHTML = subtotalPrice;
return subtotalPrice;
}

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 productElements = document.getElementsByClassName("product");

let totalPrice = 0;

for (let i = 0; i < productElements.length; i++) {
const oneProduct = productElements[i];
console.log(oneProduct);
let productTotal = updateSubtotal(oneProduct);
totalPrice += productTotal;
}

// ITERATION 3
//... your code goes here
let totalPriceElement = document.querySelector("#total-value span");
totalPriceElement.textContent = totalPrice;
}

// ITERATION 4
Expand Down