Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve() {
- let [generateTextArea, buyTextArea] = Array.from(document.getElementsByTagName("textarea"))
- let [generateBtn, buyBth] = Array.from(document.getElementsByTagName("button"))
- let tableBody = document.getElementsByTagName("tbody")[0]
- generateBtn.addEventListener("click", generateHandler)
- buyBth.addEventListener("click", buyHandler)
- function generateHandler(){
- let Items = JSON.parse(generateTextArea.value)
- for (const item of Items) {
- let newTr = document.createElement("tr")
- let tdForImg = document.createElement("td")
- let theImg = document.createElement("img")
- theImg.setAttribute("src", item.img)
- tdForImg.appendChild(theImg)
- newTr.appendChild(tdForImg)
- let tdForName = document.createElement("td")
- let theP = document.createElement("p")
- theP.textContent = item.name
- tdForName.appendChild(theP)
- newTr.appendChild(tdForName)
- let tdForPrice = document.createElement("td")
- let newP = document.createElement("p")
- newP.textContent = item.price
- tdForPrice.appendChild(newP)
- newTr.appendChild(tdForPrice)
- let tdForDecFactor = document.createElement("td")
- let finalP = document.createElement("p")
- finalP.textContent = item.decFactor
- tdForDecFactor.appendChild(finalP)
- newTr.appendChild(tdForDecFactor)
- let tdForInput = document.createElement("td")
- let input = document.createElement("input")
- input.type = "checkbox"
- tdForInput.appendChild(input)
- newTr.appendChild(tdForInput)
- tableBody.appendChild(newTr)
- }
- }
- function buyHandler(){
- let boughtFurniture = []
- let totalPrice = 0
- let totalDecorationFactorSum = 0
- let trArray = tableBody.children
- for (const tr of trArray) {
- let children = tr.children
- let tdForInput = children[4]
- let inputCheckbox = tdForInput.children[0]
- if (inputCheckbox.checked){
- let tdForName = children[1]
- let name = tdForName.children[0].textContent
- boughtFurniture.push(name)
- let tdForPrice = children[2]
- let price = Number(tdForPrice.children[0].textContent)
- totalPrice += price
- let tdFroAvFactor = children[3]
- let avFactor = Number(tdFroAvFactor.children[0].textContent)
- totalDecorationFactorSum += avFactor
- }
- }
- let ADF = totalDecorationFactorSum / boughtFurniture.length
- buyTextArea.value = `Bought furniture: ${boughtFurniture.join(", ")}` + "\n" + `Total price: ${totalPrice.toFixed(2)}` + "\n" + `Average decoration factor: ${ADF.toFixed(1)}`
- }
- }
Add Comment
Please, Sign In to add comment