Advertisement
ilianrusev

Untitled

Jan 28th, 2022
38
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.  
  3. document.getElementsByClassName('shopping-cart')[0].addEventListener('click', onClick);
  4.  
  5. let products = [];
  6. let total = 0;
  7. function onClick(ev) {
  8.  
  9. if (ev.target.className.includes('add-product')) {
  10.  
  11. let productName = ev.target.parentNode.parentNode
  12. .getElementsByClassName('product-details')[0]
  13. .getElementsByClassName('product-title')[0].textContent;
  14.  
  15. let productPrice = Number(ev.target.parentNode.parentNode
  16. .getElementsByClassName('product-line-price')[0].textContent);
  17.  
  18. if(!products.includes(productName)){
  19. products.push(productName);
  20. }
  21.  
  22. total += productPrice;
  23.  
  24. document.getElementsByTagName('TEXTAREA')[0].textContent += `Added ${productName} for ${productPrice.toFixed(2)} to the cart.\n`;
  25.  
  26.  
  27.  
  28. }else if(ev.target.className.includes('checkout')){
  29. document.getElementsByTagName('TEXTAREA')[0].textContent += `You bought ${products.join(', ')} for ${total.toFixed(2)}.`;
  30.  
  31. let buttons = Array.from(document.getElementsByTagName('BUTTON'));
  32. for (let button of buttons) {
  33. button.disabled = true;
  34. }
  35.  
  36. }
  37.  
  38. }
  39.  
  40.  
  41. }
Advertisement
RAW Paste Data Copied
Advertisement