Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.  
  3.     let textAreaElements = document.getElementsByTagName("textarea");
  4.     let buttonsElement = document.getElementsByTagName("button");
  5.     let tbody = document.getElementsByTagName("tbody")[0];
  6.     document.getElementsByTagName("input")[0].disabled = false;
  7.     buttonsElement[0].addEventListener("click", generate);
  8.     buttonsElement[1].addEventListener("click", buy);
  9.  
  10.     function generate() {
  11.  
  12.         let furnitureListInput = JSON.parse(textAreaElements[0].value);
  13.         for (let i = 0; i < furnitureListInput.length; i++) {
  14.             //tbody.appendChild(document.getElementsByTagName("tr")[1].cloneNode(true));
  15.             addFurniture(furnitureListInput[i]);
  16.         }
  17.     }
  18.  
  19.     function addFurniture(currentObj) {
  20.         let newTr = document.createElement('tr');
  21.  
  22.         let imgTd = document.createElement('td');
  23.         let imgTag = document.createElement('img');
  24.         imgTag.setAttribute('src', currentObj['img']);
  25.         imgTd.appendChild(imgTag);
  26.  
  27.         let productTd = document.createElement('td');
  28.         let productP = document.createElement('p');
  29.         productP.textContent = currentObj['name'];
  30.         productTd.appendChild(productP);
  31.  
  32.         let priceTd = document.createElement('td');
  33.         let priceP = document.createElement('p');
  34.         priceP.textContent = currentObj['price'];
  35.         priceTd.appendChild(priceP);
  36.  
  37.         let decFacTd = document.createElement('td');
  38.         let decFacP = document.createElement('p');
  39.         decFacP.textContent = currentObj['decFactor'];
  40.         decFacTd.appendChild(decFacP);
  41.  
  42.         let checkBoxTd = document.createElement('td');
  43.         let checkBoxInput = document.createElement('input');
  44.         checkBoxInput.setAttribute('type', 'checkbox');
  45.         checkBoxTd.appendChild(checkBoxInput);
  46.  
  47.         newTr.appendChild(imgTd);
  48.         newTr.appendChild(productTd);
  49.         newTr.appendChild(priceTd);
  50.         newTr.appendChild(decFacTd);
  51.         newTr.appendChild(checkBoxTd);
  52.  
  53.         tbody.appendChild(newTr);
  54.     }
  55.  
  56.     function buy() {
  57.         let furniture = [];
  58.         let totalPrice = 0;
  59.         let averageFactor = 0;
  60.         let checkbox = Array.from(document.getElementsByTagName("input"));
  61.         for (let i = 0; i < checkbox.length; i++) {
  62.             if (checkbox[i].checked) {
  63.                 let tableElements = checkbox[i].parentElement.parentElement;
  64.                 let name = tableElements.getElementsByTagName("p")[0].textContent;
  65.                 furniture.push(name);
  66.                 let price = tableElements.getElementsByTagName("p")[1].textContent;
  67.                 totalPrice += +price;
  68.                 let decFactor = tableElements.getElementsByTagName("p")[2].textContent;
  69.                 averageFactor += +decFactor;
  70.             }
  71.         }
  72.         document.getElementsByTagName("textarea")[1].textContent += `Bought furniture: ${furniture.join(", ")}\n`;
  73.         document.getElementsByTagName("textarea")[1].textContent += `Total price: ${totalPrice.toFixed(2)}\n`;
  74.         document.getElementsByTagName("textarea")[1].textContent += `Average decoration factor: ${averageFactor/furniture.length}`;
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement