Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve () {
- let textAreaElements = document.getElementsByTagName('textarea')
- let buttonsElement = document.getElementsByTagName('button')
- let tbody = document.getElementsByTagName('tbody')[0]
- document.getElementsByTagName('input')[0].disabled = false
- buttonsElement[0].addEventListener('click', generate)
- buttonsElement[1].addEventListener('click', buy)
- function generate () {
- let furnitureListInput = JSON.parse(textAreaElements[0].value)
- for (let i = 0; i < furnitureListInput.length; i++) {
- let table = document.getElementsByTagName('tr')[1].cloneNode(true)
- addFurniture(furnitureListInput[i], table)
- }
- }
- function addFurniture (furniture, table) {
- table.getElementsByTagName('img')[0].src = furniture['img']
- table.getElementsByTagName('p')[0].textContent = furniture['name']
- table.getElementsByTagName('p')[1].textContent = furniture['price']
- table.getElementsByTagName('p')[2].textContent = furniture['decFactor']
- tbody.appendChild(table)
- }
- function buy () {
- let furniture = []
- let totalPrice = 0
- let averageFactor = 0
- let checkbox = Array.from(document.getElementsByTagName('input'))
- for (let i = 0; i < checkbox.length; i++) {
- if (checkbox[i].checked) {
- let tableElements = checkbox[i].parentElement.parentElement
- let name = tableElements.getElementsByTagName('p')[0].textContent
- furniture.push(name)
- let price = tableElements.getElementsByTagName('p')[1].textContent
- totalPrice += +price
- let decFactor = tableElements.getElementsByTagName('p')[2].textContent
- averageFactor += +decFactor
- }
- }
- document.getElementsByTagName(
- 'textarea'
- )[1].value += `Bought furniture: ${furniture.join(', ')}\n`
- document.getElementsByTagName(
- 'textarea'
- )[1].value += `Total price: ${totalPrice.toFixed(2)}\n`
- document.getElementsByTagName(
- 'textarea'
- )[1].value += `Average decoration factor: ${averageFactor /
- furniture.length}`
- }
- ;(function () {
- let newTr = document.createElement('tr')
- let imgTd = document.createElement('td')
- let imgTag = document.createElement('img')
- imgTd.appendChild(imgTag)
- let productTd = document.createElement('td')
- let productP = document.createElement('p')
- productTd.appendChild(productP)
- let priceTd = document.createElement('td')
- let priceP = document.createElement('p')
- priceTd.appendChild(priceP)
- let decFacTd = document.createElement('td')
- let decFacP = document.createElement('p')
- decFacTd.appendChild(decFacP)
- let checkBoxTd = document.createElement('td')
- let checkBoxInput = document.createElement('input')
- checkBoxInput.setAttribute('type', 'checkbox')
- checkBoxTd.appendChild(checkBoxInput)
- newTr.appendChild(imgTd)
- newTr.appendChild(productTd)
- newTr.appendChild(priceTd)
- newTr.appendChild(decFacTd)
- newTr.appendChild(checkBoxTd)
- tbody.appendChild(newTr)
- })()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement