Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve() {
- let textArea = document.querySelector("body > div > textarea");
- [...document.querySelectorAll(".add-product")].forEach(add => {
- add.addEventListener("click", handle)
- });
- function handle(e) {
- let title = e.target.parentElement.previousElementSibling.firstElementChild.innerText;
- let price = e.target.parentElement.nextElementSibling.innerText;
- textArea.value += `Added ${title} for ${price} to the cart.\n`;
- }
- document.querySelector("body > div > button").addEventListener("click", buy);
- function buy(){
- let finalText = textArea.value.split('\n').filter(x=> x.length> 0);
- let boughtProducts = [];
- let totalPrice = 0;
- for (const bought of finalText) {
- boughtProducts.push(bought.split(' ')[1]);
- totalPrice+= +(bought.split(' ')[3]);
- }
- textArea.value += `You bought ${boughtProducts.join(', ')} for ${totalPrice.toFixed(2)}.`;
- Array.from(document.querySelectorAll("button")).map(x => x.disabled=true);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement