Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve() {
- let allButtons = Array.from(document.getElementsByClassName('add-product'));
- let textAreaElement = document.getElementsByTagName('textarea')[0];
- let totalPrice = 0;
- let listOfItems = [];
- allButtons[0].addEventListener('click', (f) => {
- let money = Number(document.getElementsByClassName('product-line-price')[0].textContent);
- textAreaElement.textContent += `Added Bread for ${money.toFixed(2)} to the cart.\n`;
- totalPrice += money;
- if (!listOfItems.includes('Bread')) {
- listOfItems.push('Bread');
- }
- })
- allButtons[1].addEventListener('click', (f) => {
- let money = +document.getElementsByClassName('product-line-price')[1].textContent;
- textAreaElement.textContent += `Added Milk for ${money.toFixed(2)} to the cart.\n`;
- totalPrice += money;
- if (!listOfItems.includes('Milk')) {
- listOfItems.push('Milk');
- }
- })
- allButtons[2].addEventListener('click', (f) => {
- let money = +document.getElementsByClassName('product-line-price')[2].textContent;
- textAreaElement.textContent += `Added Tomatoes for ${money.toFixed(2)} to the cart.\n`;
- totalPrice += money;
- if (!listOfItems.includes('Tomatoes')) {
- listOfItems.push('Tomatoes');
- }
- })
- let checkoutButton = document.getElementsByClassName('checkout')[0];
- checkoutButton.addEventListener('click', () => {
- textAreaElement.textContent += `You bought ${listOfItems.join(', ')} for ${totalPrice.toFixed(2)}.`;
- allButtons[0].disabled = true;
- allButtons[1].disabled = true;
- allButtons[2].disabled = true;
- checkoutButton.disabled = true;
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement