krasizorbov

Shopping Cart

Aug 25th, 2020 (edited)
1,612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.   let products = document.getElementsByClassName("product");
  3.   let textArea = document.getElementsByTagName("textarea")[0];
  4.   let check = document.querySelector(".checkout");
  5.   let totalPrice = 0;
  6.   let productList = [];
  7.   Array.from(products).forEach((p) =>
  8.     p.addEventListener("click", function () {
  9.       let name = p.getElementsByClassName("product-title")[0].textContent;
  10.       let price = p.getElementsByClassName("product-line-price")[0].textContent;
  11.       if (!productList.includes(name)) {
  12.         productList.push(name);
  13.       }
  14.       totalPrice += +price;
  15.       textArea.value += `Added ${name} for ${price} to the cart.\n`;
  16.     })
  17.   );
  18.   check.addEventListener("click", function () {
  19.     let list = productList.join(", ");
  20.     totalPrice = totalPrice.toFixed(2);
  21.     textArea.value += `You bought ${list} for ${totalPrice}.`;
  22.     let buttons = Array.from(document.querySelectorAll("button"));
  23.     buttons.forEach((button) => (button.disabled = true));
  24.   });
  25. }
Advertisement
Add Comment
Please, Sign In to add comment