SHARE
TWEET

Untitled

a guest Feb 2nd, 2020 145 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     let button = document.getElementsByTagName("button")
  3.     let textArea = document.getElementsByTagName("textarea")[0]
  4.  
  5.     let list = {}
  6.  
  7.     Array.from(button).forEach(b => {
  8.         b.addEventListener("click", (e) => {
  9.             if (e.target.className === "add-product") {
  10.                 let currentElement = e.target.parentElement
  11.  
  12.                 let priceElement = Number(currentElement.nextElementSibling.innerHTML)
  13.                 let brandElement = currentElement.previousElementSibling.children[0].innerHTML
  14.  
  15.                 if (!list[brandElement]) {
  16.                     list[brandElement] = 0
  17.                 }
  18.                 list[brandElement] += priceElement
  19.  
  20.                 textArea.innerHTML += `Added ${brandElement} for ${priceElement.toFixed(2)} to the cart.\n`
  21.             } else {
  22.  
  23.                 let totalPrice = Number(Object.values(list).reduce((a,b)=>a+b))                
  24.                 textArea.innerHTML+=`You bought ${Object.keys(list).join(', ')} for ${totalPrice.toFixed(2)}.`
  25.                
  26.                 Array.from(button).forEach(b => b.disabled = true)
  27.  
  28.             }
  29.         })
  30.     })
  31. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top