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

week 3 (day 6) lab Complete #81

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
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
97 changes: 90 additions & 7 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

function updateSubtotal(product) {
console.log('Calculating subtotal, yey!');
let price = Number(product.querySelector('.price span').innerText);
let quantity = Number(product.querySelector('.quantity input').value);
let subtotal = price * quantity;
product.querySelector('.subtotal span').innerText = subtotal;
return subtotal;

//... your code goes here
}

function calculateAll() {
Expand All @@ -14,29 +18,108 @@ function calculateAll() {
// end of test

// ITERATION 2
//... your code goes here
let products = document.getElementsByClassName('product');
for (let i = 0; i < products.length; i++) {
updateSubtotal(products[i]);
}

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

}

// ITERATION 4

function removeProduct(event) {
const target = event.currentTarget;
console.log('The target in remove is:', target);
//... your code goes here
let targetProduct = target.parentNode.parentNode;
let targetCart = targetProduct.parentNode;
console.log(targetCart);
document.querySelector('#total-value span').innerHTML =
Number(document.querySelector('#total-value span').innerHTML) -
Number(targetProduct.querySelector('.subtotal span').innerHTML);
targetCart.removeChild(targetProduct);
}

// ITERATION 5

function createProduct() {
//... your code goes here
function createProduct(event) {
let target = event.currentTarget;
let parentTarget = target.parentNode.parentNode;
let proName = parentTarget
.getElementsByTagName('td')[0]
.getElementsByTagName('input')[0].value;
let price = parseFloat(
parentTarget.getElementsByTagName('td')[1].getElementsByTagName('input')[0]
.value
).toFixed(2);

let nameHolder = document.createElement('td');
let nameTag = document.createElement('span');
let nameText = document.createTextNode(proName);
nameTag.appendChild(nameText);
nameHolder.appendChild(nameTag);
nameHolder.className = 'name';

let priceHolder = document.createElement('td');
priceHolder.innerHTML = '$';
let priceTag = document.createElement('span');
let priceText = document.createTextNode(price);
priceTag.appendChild(priceText);
priceHolder.appendChild(priceTag);
priceHolder.className = 'price';

let quantityHolder = document.createElement('td');
let Input = document.createElement('input');
Input.type = 'number';
Input.defaultValue = '0';
Input.min = '0';
Input.placeholder = 'Quantity';
quantityHolder.appendChild(Input);
quantityHolder.className = 'quantity';

let subtotalHolder = document.createElement('td');
subtotalHolder.innerHTML = '$';
let subtotalTag = document.createElement('span');
subtotalTag.innerHTML = 0;
subtotalHolder.appendChild(subtotalTag);
subtotalHolder.className = 'subtotal';

let actionHolder = document.createElement('td');
let buttonTag = document.createElement('button');
buttonTag.innerHTML = 'Remove';
actionHolder.appendChild(buttonTag);
actionHolder.className = 'action';
buttonTag.className = 'btn btn-remove';
buttonTag.addEventListener('click', removeProduct);

let productHolder = document.createElement('tr');
productHolder.appendChild(nameHolder);
productHolder.appendChild(priceHolder);
productHolder.appendChild(quantityHolder);
productHolder.appendChild(subtotalHolder);
productHolder.appendChild(actionHolder);
productHolder.className = 'product';

let prodList = document.getElementsByTagName('tbody')[0];
prodList.appendChild(productHolder);
}

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

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

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