Anonimka

Untitled

May 21st, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.  
  3.    let [button1,button2,button3]=document.querySelectorAll('.add-product');
  4.    let textArea=document.getElementsByTagName('textarea')[0];
  5.    let checkoutBtn=document.querySelector('.checkout');
  6.    let [name1,name2,name3]=document.querySelectorAll('.product-title');
  7.    let [price1,price2,price3]=document.querySelectorAll('.product-line-price');
  8.    let boughtNames=[];
  9.    let totalPrice=0;
  10.  
  11.    button1.addEventListener('click',()=>{
  12.       textArea.textContent+=`Added ${name1.textContent} for ${price1.textContent} to the cart.\n`;
  13.       if(!boughtNames.includes(name1.textContent)){
  14.          boughtNames.push(name1.textContent);
  15.       }
  16.       totalPrice+=Number(price1.textContent);
  17.    });
  18.  
  19.    button2.addEventListener('click',()=>{
  20.       textArea.textContent+=`Added ${name2.textContent} for ${price2.textContent} to the cart.\n`;
  21.       if(!boughtNames.includes(name2.textContent)){
  22.          boughtNames.push(name2.textContent);
  23.       }
  24.       totalPrice+=Number(price2.textContent);
  25.    });
  26.  
  27.    button3.addEventListener('click',()=>{
  28.       textArea.textContent+=`Added ${name3.textContent} for ${price3.textContent} to the cart.\n`;
  29.       if(!boughtNames.includes(name3.textContent)){
  30.          boughtNames.push(name3.textContent);
  31.       }
  32.       totalPrice+=Number(price3.textContent);
  33.    });
  34.  
  35.    checkoutBtn.addEventListener('click',()=>{
  36.       let buttons=document.querySelectorAll('.add-product');
  37.       for (let i = 0; i < buttons.length; i++) {
  38.          buttons[i].disabled = true;
  39.       }
  40.       textArea.textContent+=`You bought ${boughtNames.join(', ')} for ${totalPrice.toFixed(2)}.`
  41.    });
  42. }
Advertisement
Add Comment
Please, Sign In to add comment