Advertisement
nikolapetkov824

Shooping Cart

Jul 6th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.    let allButtons = Array.from(document.getElementsByClassName('add-product'));
  3.    let textAreaElement = document.getElementsByTagName('textarea')[0];
  4.  
  5.    let totalPrice = 0;
  6.    let listOfItems = [];
  7.  
  8.    allButtons[0].addEventListener('click', (f) => {
  9.       let money = Number(document.getElementsByClassName('product-line-price')[0].textContent);
  10.       textAreaElement.textContent += `Added Bread for ${money.toFixed(2)} to the cart.\n`;
  11.       totalPrice += money;
  12.       if (!listOfItems.includes('Bread')) {
  13.          listOfItems.push('Bread');
  14.       }
  15.    })
  16.  
  17.    allButtons[1].addEventListener('click', (f) => {
  18.       let money = +document.getElementsByClassName('product-line-price')[1].textContent;
  19.       textAreaElement.textContent += `Added Milk for ${money.toFixed(2)} to the cart.\n`;
  20.       totalPrice += money;
  21.       if (!listOfItems.includes('Milk')) {
  22.          listOfItems.push('Milk');
  23.       }
  24.    })
  25.  
  26.    allButtons[2].addEventListener('click', (f) => {
  27.       let money = +document.getElementsByClassName('product-line-price')[2].textContent;
  28.       textAreaElement.textContent += `Added Tomatoes for ${money.toFixed(2)} to the cart.\n`;
  29.       totalPrice += money;
  30.       if (!listOfItems.includes('Tomatoes')) {
  31.          listOfItems.push('Tomatoes');
  32.       }
  33.    })
  34.  
  35.    let checkoutButton = document.getElementsByClassName('checkout')[0];
  36.  
  37.    checkoutButton.addEventListener('click', () => {
  38.       textAreaElement.textContent += `You bought ${listOfItems.join(', ')} for ${totalPrice.toFixed(2)}.`;
  39.       allButtons[0].disabled = true;
  40.       allButtons[1].disabled = true;
  41.       allButtons[2].disabled = true;
  42.       checkoutButton.disabled = true;
  43.    })
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement