Advertisement
bebo231312312321

Untitled

May 28th, 2023
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     let [generate, buy] = document.getElementsByTagName("button")
  3.     generate.addEventListener("click", () => {
  4.         let products = JSON.parse(document.querySelectorAll("textarea")[0].value)
  5.  
  6.         products.forEach(product => {
  7.             let { name, img, price, decFactor } = product
  8.             let tr = `<tr>\n
  9.             <td><img src=${img}></td>\n
  10.             <td><p>${name}</p></td>\n
  11.             <td><p>${price}</p></td>\n
  12.             <td><p>${decFactor}</p></td>\n
  13.             <td><input type="checkbox"/></td>\n
  14.             </tr>`
  15.             document.querySelectorAll("tbody")[0].insertAdjacentHTML("beforeend", tr)
  16.         })
  17.         document.querySelectorAll("textarea")[0].textContent=""
  18.     })
  19.     document.querySelectorAll("textarea")[0].value = ""
  20.     buy.addEventListener("click", () => {
  21.         let [products, prices, factors] = [[], [], []]
  22.         Array.from(document.getElementsByTagName("input")).forEach(x => {
  23.             if (x.checked) {
  24.                 let parentElement = x.parentElement.parentElement
  25.                 let [name, price, factor] = parentElement.querySelectorAll("td p")
  26.                 name = name.textContent
  27.                 price = Number(price.textContent)
  28.                 factor = Number(factor.textContent)
  29.  
  30.                 products.push(name)
  31.                 prices.push(price)
  32.                 factors.push(factor)
  33.             }
  34.         })
  35.         let totalPrice = prices.reduce((a, b) => a + b, 0)
  36.         let factorial = factors.reduce((a, b) => a + b, 0) / factors.length
  37.         document.querySelectorAll("textarea")[1].textContent = `Bought furniture: ${products.join(", ")}\nTotal price: ${totalPrice.toFixed(2)}\nAverage decoration factor: ${factorial}`
  38.     })
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement