Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. function solve() {
  2. let textArea = document.querySelector("body > div > textarea");
  3. [...document.querySelectorAll(".add-product")].forEach(add => {
  4. add.addEventListener("click", handle)
  5. });
  6. function handle(e) {
  7. let title = e.target.parentElement.previousElementSibling.firstElementChild.innerText;
  8. let price = e.target.parentElement.nextElementSibling.innerText;
  9.  
  10. textArea.value += `Added ${title} for ${price} to the cart.\n`;
  11. }
  12. document.querySelector("body > div > button").addEventListener("click", buy);
  13. function buy(){
  14. let finalText = textArea.value.split('\n').filter(x=> x.length> 0);
  15. let boughtProducts = [];
  16. let totalPrice = 0;
  17. for (const bought of finalText) {
  18. boughtProducts.push(bought.split(' ')[1]);
  19. totalPrice+= +(bought.split(' ')[3]);
  20. }
  21. textArea.value += `You bought ${boughtProducts.join(', ')} for ${totalPrice.toFixed(2)}.`;
  22. Array.from(document.querySelectorAll("button")).map(x => x.disabled=true);
  23. }
  24. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement