Advertisement
TZinovieva

Shopping Cart JS Advanced

Sep 28th, 2023 (edited)
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     const cartTextarea = document.querySelector('textarea');
  3.     const checkoutButton = document.querySelector('.checkout');
  4.     const addButtons = document.querySelectorAll('.add-product');
  5.  
  6.     const addedProducts = {};
  7.  
  8.     for (let index = 0; index < addButtons.length; index++) {
  9.         addButtons[index].addEventListener('click', function() {
  10.             const product = document.querySelectorAll('.product')[index];
  11.             const productName = product.querySelector('.product-title').textContent;
  12.             const productPrice = Number(product.querySelector('.product-line-price').textContent);
  13.  
  14.             if (addedProducts[productName]) {
  15.                 addedProducts[productName].quantity++;
  16.             } else {
  17.                 addedProducts[productName] = {
  18.                     price: productPrice,
  19.                     quantity: 1,
  20.                 };
  21.             }
  22.  
  23.             cartTextarea.value += `Added ${productName} for ${productPrice.toFixed(2)} to the cart.\n`;
  24.         });
  25.     }
  26.  
  27.     checkoutButton.addEventListener('click', function() {
  28.         let totalPrice = 0;
  29.         const uniqueProducts = [];
  30.  
  31.         for (const productName in addedProducts) {
  32.             if (addedProducts.hasOwnProperty(productName)) {
  33.                 const product = addedProducts[productName];
  34.                 totalPrice += product.price * product.quantity;
  35.                 uniqueProducts.push(productName);
  36.             }
  37.         }
  38.  
  39.         cartTextarea.value += `You bought ${uniqueProducts.join(', ')} for ${totalPrice.toFixed(2)}.`;
  40.  
  41.         for (let index = 0; index < addButtons.length; index++) {
  42.             addButtons[index].disabled = true;
  43.         }
  44.         checkoutButton.disabled = true;
  45.     });
  46. }
  47.  
  48. OR
  49.  
  50. function solve() {
  51.    const products = document.querySelectorAll('.product');
  52.    const cartTextarea = document.querySelector('textarea');
  53.    const checkoutButton = document.querySelector('.checkout');
  54.    const addButtons = Array.from(document.querySelectorAll('.add-product'));
  55.  
  56.    const addedProducts = [];
  57.  
  58.    addButtons.forEach((button, index) => {
  59.        button.addEventListener('click', () => {
  60.            const product = products[index];
  61.            const productName = product.querySelector('.product-title').textContent;
  62.            const productPrice = Number(product.querySelector('.product-line-price').textContent);
  63.            
  64.            addedProducts.push({ name: productName, price: productPrice.toFixed(2) });
  65.            
  66.            cartTextarea.value += `Added ${productName} for ${productPrice.toFixed(2)} to the cart.\n`;
  67.        });
  68.    });
  69.  
  70.    checkoutButton.addEventListener('click', () => {
  71.        const totalPrice = addedProducts.reduce((total, product) => total + parseFloat(product.price), 0);
  72.        const uniqueProducts = [...new Set(addedProducts.map(product => product.name))];
  73.  
  74.        cartTextarea.value += `You bought ${uniqueProducts.join(', ')} for ${totalPrice.toFixed(2)}.`;
  75.  
  76.        addButtons.forEach(button => button.disabled = true);
  77.        checkoutButton.disabled = true;
  78.    });
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement