Advertisement
didkoslawow

Untitled

Apr 24th, 2023
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     Array.from(document.querySelectorAll('.add-product')).forEach(x => x.addEventListener('click', addToCart));
  3.     document.querySelector('.checkout').addEventListener('click', checkout);
  4.  
  5.     const buttons = Array.from(document.querySelectorAll('button'));
  6.     const textArea = document.querySelector('textarea');
  7.     const products = new Set();
  8.     let total = [];
  9.      
  10.     function addToCart(e) {
  11.        const productName = e.target.parentElement.parentElement.querySelector('.product-title').textContent;
  12.        const productPrice = e.target.parentElement.parentElement.querySelector('.product-line-price').textContent;
  13.        
  14.        textArea.textContent += `Added ${productName} for ${productPrice} to the cart.\n`
  15.  
  16.        if (!products.has(productName)) products.add(productName);
  17.        total.push(Number(productPrice));
  18.  
  19.     }
  20.  
  21.     function checkout() {
  22.        const totalPrice = total.reduce((acc, x) => acc + x, 0);
  23.        textArea.textContent += `You bought ${Array.from(products).join(', ')} for ${totalPrice.toFixed(2)}.`;
  24.  
  25.        buttons.forEach(b => b.disabled = true);
  26.     }  
  27.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement