Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve() {
- let [button1,button2,button3]=document.querySelectorAll('.add-product');
- let textArea=document.getElementsByTagName('textarea')[0];
- let checkoutBtn=document.querySelector('.checkout');
- let [name1,name2,name3]=document.querySelectorAll('.product-title');
- let [price1,price2,price3]=document.querySelectorAll('.product-line-price');
- let boughtNames=[];
- let totalPrice=0;
- button1.addEventListener('click',()=>{
- textArea.textContent+=`Added ${name1.textContent} for ${price1.textContent} to the cart.\n`;
- if(!boughtNames.includes(name1.textContent)){
- boughtNames.push(name1.textContent);
- }
- totalPrice+=Number(price1.textContent);
- });
- button2.addEventListener('click',()=>{
- textArea.textContent+=`Added ${name2.textContent} for ${price2.textContent} to the cart.\n`;
- if(!boughtNames.includes(name2.textContent)){
- boughtNames.push(name2.textContent);
- }
- totalPrice+=Number(price2.textContent);
- });
- button3.addEventListener('click',()=>{
- textArea.textContent+=`Added ${name3.textContent} for ${price3.textContent} to the cart.\n`;
- if(!boughtNames.includes(name3.textContent)){
- boughtNames.push(name3.textContent);
- }
- totalPrice+=Number(price3.textContent);
- });
- checkoutBtn.addEventListener('click',()=>{
- let buttons=document.querySelectorAll('.add-product');
- for (let i = 0; i < buttons.length; i++) {
- buttons[i].disabled = true;
- }
- textArea.textContent+=`You bought ${boughtNames.join(', ')} for ${totalPrice.toFixed(2)}.`
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment