nikolayneykov

Untitled

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