krasizorbov

Furniture

Oct 16th, 2020
2,350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.   let [generateBtn, buyBtn] = document.querySelectorAll('button');
  3.   generateBtn.addEventListener('click', generate);
  4.   buyBtn.addEventListener('click', buy);
  5.  
  6.  
  7.   let inputTextBox = document.getElementsByTagName('textarea')[0];
  8.   let resultTextBox = document.getElementsByTagName('textarea')[1];
  9.  
  10.   let furnatureList = document.getElementsByTagName('tbody')[0];
  11.  
  12.   function generate() {
  13.     let furnetureArrayInput = JSON.parse(inputTextBox.value);
  14.     furnetureArrayInput.forEach(obj => {
  15.       let {img, name, price, decFactor} = obj;
  16.       let newRow = furnatureList.insertRow(-1);
  17.       let imgCell = newRow.insertCell(0);
  18.       let nameCell = newRow.insertCell(1);
  19.       let priceCell = newRow.insertCell(2);
  20.       let decFactorCell = newRow.insertCell(3);
  21.       let checkBoxCell = newRow.insertCell(4);
  22.  
  23.       let imgElement = document.createElement('img');
  24.       imgElement.src = img;
  25.       imgCell.appendChild(imgElement);
  26.  
  27.       let nameElement = document.createElement('p');
  28.       nameElement.textContent = name;
  29.       nameCell.appendChild(nameElement);
  30.  
  31.       let priceElement = document.createElement('p');
  32.       priceElement.textContent = price;
  33.       priceCell.appendChild(priceElement);
  34.  
  35.       let decFactorElement = document.createElement('p');
  36.       decFactorElement.textContent = decFactor;
  37.       decFactorCell.appendChild(decFactorElement);
  38.  
  39.       let checkBoxElement = document.createElement('input');
  40.       checkBoxElement.type = 'checkbox';
  41.       checkBoxCell.appendChild(checkBoxElement);
  42.     });
  43.     inputTextBox.value = "";
  44.   }
  45.  
  46.   function buy() {
  47.     let boughtFurniture = [];
  48.     let totalPrice = 0;
  49.     let sumDecFactor = 0;
  50.     let counter = 0;
  51.  
  52.           for (row of furnatureList.querySelectorAll("tr")) {
  53.             if (row.querySelectorAll("input")[0].checked) {
  54.               const productInfo = row.querySelectorAll("p");
  55.               boughtFurniture.push(productInfo[0].textContent);
  56.               totalPrice += Number(productInfo[1].textContent);
  57.               sumDecFactor += Number(productInfo[2].textContent);
  58.               counter++;
  59.             }
  60.           }
  61.    
  62.     let avgDecFactor = sumDecFactor / counter || 1;
  63.     let output = `Bought furniture: ${boughtFurniture.join(', ')}\n`;
  64.     output += `Total price: ${totalPrice.toFixed(2)}\n`;
  65.     output += `Average decoration factor: ${avgDecFactor}`;
  66.     resultTextBox.value = output;
  67.  
  68.   }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment