Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve() {
- let textAreaElements = document.getElementsByTagName("textarea");
- let buttonsElement = document.getElementsByTagName("button");
- let tbody = document.getElementsByTagName("tbody")[0];
- document.getElementsByTagName("input")[0].disabled = false;
- buttonsElement[0].addEventListener("click", generate);
- buttonsElement[1].addEventListener("click", buy);
- function generate() {
- let furnitureListInput = JSON.parse(textAreaElements[0].value);
- for (let i = 0; i < furnitureListInput.length; i++) {
- //tbody.appendChild(document.getElementsByTagName("tr")[1].cloneNode(true));
- addFurniture(furnitureListInput[i]);
- }
- }
- function addFurniture(currentObj) {
- let newTr = document.createElement('tr');
- let imgTd = document.createElement('td');
- let imgTag = document.createElement('img');
- imgTag.setAttribute('src', currentObj['img']);
- imgTd.appendChild(imgTag);
- let productTd = document.createElement('td');
- let productP = document.createElement('p');
- productP.textContent = currentObj['name'];
- productTd.appendChild(productP);
- let priceTd = document.createElement('td');
- let priceP = document.createElement('p');
- priceP.textContent = currentObj['price'];
- priceTd.appendChild(priceP);
- let decFacTd = document.createElement('td');
- let decFacP = document.createElement('p');
- decFacP.textContent = currentObj['decFactor'];
- decFacTd.appendChild(decFacP);
- let checkBoxTd = document.createElement('td');
- let checkBoxInput = document.createElement('input');
- checkBoxInput.setAttribute('type', 'checkbox');
- checkBoxTd.appendChild(checkBoxInput);
- newTr.appendChild(imgTd);
- newTr.appendChild(productTd);
- newTr.appendChild(priceTd);
- newTr.appendChild(decFacTd);
- newTr.appendChild(checkBoxTd);
- tbody.appendChild(newTr);
- }
- function buy() {
- let furniture = [];
- let totalPrice = 0;
- let averageFactor = 0;
- let checkbox = Array.from(document.getElementsByTagName("input"));
- for (let i = 0; i < checkbox.length; i++) {
- if (checkbox[i].checked) {
- let tableElements = checkbox[i].parentElement.parentElement;
- let name = tableElements.getElementsByTagName("p")[0].textContent;
- furniture.push(name);
- let price = tableElements.getElementsByTagName("p")[1].textContent;
- totalPrice += +price;
- let decFactor = tableElements.getElementsByTagName("p")[2].textContent;
- averageFactor += +decFactor;
- }
- }
- document.getElementsByTagName("textarea")[1].textContent += `Bought furniture: ${furniture.join(", ")}\n`;
- document.getElementsByTagName("textarea")[1].textContent += `Total price: ${totalPrice.toFixed(2)}\n`;
- document.getElementsByTagName("textarea")[1].textContent += `Average decoration factor: ${averageFactor/furniture.length}`;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement