Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve () {
- let inputTextArea = document.querySelector('textarea')
- let productsTable = document.querySelector('tbody')
- let generateButton = document.querySelector('button')
- generateButton.addEventListener('click', function () {
- let object = JSON.parse(inputTextArea.value)
- for (let i = 0; i < object.length; i++) {
- // creates new row
- let newTrElement = document.createElement('tr')
- productsTable.appendChild(newTrElement)
- // creates a cell for the image
- let imageTd = document.createElement('td')
- let image = document.createElement('img')
- image.src = object[i].img
- imageTd.appendChild(image)
- newTrElement.appendChild(imageTd)
- // creates a cell for the name
- let nameTd = document.createElement('td')
- let name = document.createElement('p')
- name.textContent = object[i].name
- nameTd.appendChild(name)
- newTrElement.appendChild(nameTd)
- // creates a cell for the price
- let priceTd = document.createElement('td')
- let price = document.createElement('p')
- price.textContent = object[i].price
- priceTd.appendChild(price)
- newTrElement.appendChild(priceTd)
- // creates a cell for the decFaftor
- let decfacTd = document.createElement('td')
- let decfact = document.createElement('p')
- decfact.textContent = object[i].decFactor
- decfacTd.appendChild(decfact)
- newTrElement.appendChild(decfacTd)
- // creates a checkable checkbox..the default one isn`t
- let checkBoxTd = document.createElement('td')
- let checkBox = document.createElement('input')
- checkBox.type = 'checkbox'
- checkBox.disabled = false
- checkBoxTd.appendChild(checkBox)
- newTrElement.appendChild(checkBoxTd)
- }
- })
- let buyButton = document.querySelectorAll('button')[1]
- buyButton.addEventListener('click', function () {
- let allTr = document.querySelectorAll('tr')
- let allFurniture = []
- let allDecFacts = 0
- let totalPrice = 0
- for (let i = 2; i < allTr.length; i++) {
- let currentCheckBox = allTr[i].querySelector('input')
- if (currentCheckBox.checked) {
- allFurniture.push(allTr[i].children[1].textContent)
- allDecFacts += +allTr[i].children[3].textContent
- totalPrice += +allTr[i].children[2].textContent
- }
- }
- let averageDecFact = allDecFacts / allFurniture.length
- document.getElementsByTagName('textarea')[1].value =
- 'Bought furniture: ' +
- allFurniture.join(', ') +
- '\nTotal price: ' +
- totalPrice.toFixed(2) +
- '\nAverage decoration factor: ' +
- averageDecFact
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment