Advertisement
nikolayneykov

Untitled

May 22nd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve () {
  2.   let buttons = [...document.querySelectorAll('.add-product')]
  3.   buttons.forEach(b => b.addEventListener('click', add))
  4.  
  5.   document.querySelector('.checkout').addEventListener('click', checkout)
  6.   let totalPrice = 0
  7.   let list = {}
  8.  
  9.   let textArea = document.querySelector('textArea')
  10.  
  11.   function add () {
  12.     let product = this.parentNode.parentNode
  13.     let name = product.querySelector('.product-title').textContent
  14.     let money = +product.querySelector('.product-line-price').textContent
  15.     list[name] = name
  16.     totalPrice += money
  17.     textArea.value += `Added ${name} for ${money.toFixed(2)} to the cart.\n`
  18.   }
  19.  
  20.   function checkout () {
  21.     textArea.value += `You bought ${Object.values(list).join(
  22.       ', '
  23.     )} for ${totalPrice.toFixed(2)}.`
  24.  
  25.     buttons.forEach(b => b.removeEventListener('click', add))
  26.     this.removeEventListener('click', checkout)
  27.   }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement