Advertisement
Guest User

Untitled

a guest
May 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     let inputTextArea = document.querySelector('textarea');
  3.     let productsTable = document.querySelector('tbody');
  4.  
  5.     let generateButton = document.querySelector('button');
  6.     generateButton.addEventListener('click', function () {
  7.         let object = JSON.parse(inputTextArea.value);
  8. // creates new row
  9.         let newTrElement = document.createElement('tr');
  10.         productsTable.appendChild(newTrElement)
  11.  
  12. // creates a cell for the image
  13.         let imageTd = document.createElement('td');
  14.         let image = document.createElement('img');
  15.         image.src = object[0].img;
  16.         imageTd.appendChild(image);
  17.  
  18.         newTrElement.appendChild(imageTd);
  19.  
  20. //creates a cell for the name
  21.         let nameTd = document.createElement('td');
  22.         let name = document.createElement('p');
  23.         name.textContent = object[0].name;
  24.         nameTd.appendChild(name);
  25.  
  26.         newTrElement.appendChild(nameTd);
  27.  
  28. //creates a cell for the price
  29.         let priceTd = document.createElement('td');
  30.         let price = document.createElement('p');
  31.         price.textContent = object[0].price;
  32.         priceTd.appendChild(price);
  33.  
  34.         newTrElement.appendChild(priceTd);
  35.  
  36. //creates a cell for the decFaftor
  37.         let decfacTd = document.createElement('td');
  38.         let decfact = document.createElement('p');
  39.         decfact.textContent = object[0].decFactor;
  40.         decfacTd.appendChild(decfact);
  41.  
  42.         newTrElement.appendChild(decfacTd);
  43.  
  44. //creates a checkable checkbox..the default one isn`t
  45.         let checkBoxTd = document.createElement('td');
  46.         let checkBox = document.createElement('input');
  47.         checkBox.type = 'checkbox';
  48.         checkBox.disabled = false;
  49.         checkBoxTd.appendChild(checkBox);
  50.  
  51.         newTrElement.appendChild(checkBoxTd);
  52.     })
  53.  
  54.  
  55.     let buyButton = document.querySelectorAll('button')[1];
  56.  
  57.  
  58.  
  59. buyButton.addEventListener('click', function () {
  60.     let allTr = document.querySelectorAll('tr');
  61.     let allFurniture = [];
  62.     let allDecFacts = 0;
  63.     let totalPrice = 0;
  64.     for(let i = 2;i < allTr.length;i++){
  65.         let currentCheckBox = allTr[i].querySelector('input');
  66.         if(currentCheckBox.checked){
  67.            allFurniture.push(allTr[i].children[1].textContent);
  68.            allDecFacts += +allTr[i].children[3].textContent;
  69.            totalPrice += +allTr[i].children[2].textContent;
  70.         }
  71.     }
  72.  
  73.     let averageDecFact = allDecFacts / allFurniture.length;
  74.  
  75.     document
  76.         .getElementsByTagName('textarea')[1]
  77.         .value =
  78.         "Bought furniture: " +
  79.         allFurniture.join(', ') +
  80.         "\nTotal price: " +
  81.         totalPrice.toFixed(2) +
  82.         "\nAverage decoration factor: " +
  83.         averageDecFact;
  84. })
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement