Advertisement
Maxim_01

Untitled

Mar 18th, 2023
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.  
  3.   let [generateTextArea, buyTextArea] = Array.from(document.getElementsByTagName("textarea"))
  4.   let [generateBtn, buyBth] = Array.from(document.getElementsByTagName("button"))
  5.  
  6.   let tableBody = document.getElementsByTagName("tbody")[0]
  7.  
  8.   generateBtn.addEventListener("click", generateHandler)
  9.   buyBth.addEventListener("click", buyHandler)
  10.  
  11.   function generateHandler(){
  12.     let Items = JSON.parse(generateTextArea.value)
  13.  
  14.     for (const item of Items) {
  15.       let newTr = document.createElement("tr")
  16.  
  17.       let tdForImg = document.createElement("td")
  18.       let theImg = document.createElement("img")  
  19.       theImg.setAttribute("src", item.img)
  20.       tdForImg.appendChild(theImg)
  21.       newTr.appendChild(tdForImg)
  22.  
  23.       let tdForName = document.createElement("td")
  24.       let theP = document.createElement("p")
  25.       theP.textContent = item.name
  26.       tdForName.appendChild(theP)
  27.       newTr.appendChild(tdForName)
  28.  
  29.       let tdForPrice = document.createElement("td")
  30.       let newP = document.createElement("p")
  31.       newP.textContent = item.price
  32.       tdForPrice.appendChild(newP)
  33.       newTr.appendChild(tdForPrice)
  34.  
  35.       let tdForDecFactor = document.createElement("td")
  36.       let finalP = document.createElement("p")
  37.       finalP.textContent = item.decFactor
  38.       tdForDecFactor.appendChild(finalP)
  39.       newTr.appendChild(tdForDecFactor)
  40.  
  41.       let tdForInput = document.createElement("td")
  42.       let input = document.createElement("input")
  43.       input.type = "checkbox"
  44.       tdForInput.appendChild(input)
  45.       newTr.appendChild(tdForInput)
  46.  
  47.       tableBody.appendChild(newTr)
  48.     }
  49.   }
  50.  
  51.   function buyHandler(){
  52.  
  53.     let boughtFurniture = []
  54.     let totalPrice = 0
  55.     let totalDecorationFactorSum = 0
  56.  
  57.     let trArray = tableBody.children
  58.  
  59.     for (const tr of trArray) {
  60.  
  61.       let children = tr.children
  62.       let tdForInput = children[4]
  63.       let inputCheckbox = tdForInput.children[0]
  64.  
  65.       if (inputCheckbox.checked){
  66.  
  67.         let tdForName = children[1]
  68.         let name = tdForName.children[0].textContent
  69.         boughtFurniture.push(name)
  70.  
  71.         let tdForPrice = children[2]
  72.         let price = Number(tdForPrice.children[0].textContent)
  73.         totalPrice += price
  74.  
  75.         let tdFroAvFactor = children[3]
  76.         let avFactor = Number(tdFroAvFactor.children[0].textContent)
  77.         totalDecorationFactorSum += avFactor
  78.       }
  79.     }
  80.  
  81.     let ADF = totalDecorationFactorSum / boughtFurniture.length
  82.    
  83.     buyTextArea.value = `Bought furniture: ${boughtFurniture.join(", ")}` + "\n" + `Total price: ${totalPrice.toFixed(2)}` + "\n" + `Average decoration factor: ${ADF.toFixed(1)}`
  84.    
  85.   }
  86.  
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement