Advertisement
petur_stoqnov

Untitled

Jan 30th, 2021
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     // деструкториране на querySelector
  3.     let [generateButton, buyButton] = [...document.querySelectorAll('button')];
  4.     let [textArea1, textArea2] = [...document.querySelectorAll('textarea')];
  5.  
  6.     generateButton.addEventListener('click', () => {
  7.         let products = JSON.parse(textArea1.value);
  8.         products.forEach(o => {
  9.             // деструкториране на обект
  10.             let {img, name, price, decFactor} = o;
  11.             // създаване на htmlString
  12.             let htmlString = `<tr><td>
  13.                               <img src="${img}">
  14.                                </td><td><p>${name}</p>
  15.                                <td><p>${Number(price)}</p></td>
  16.                                 <td><p>${Number(decFactor)}</p></td>
  17.                                <td><input type="checkbox"/></td>
  18.                                </tr>`;
  19.             // добавяне на htmlString към html-а на страницата
  20.             let tBody = document.querySelector('tbody');
  21.             tBody.insertAdjacentHTML('beforeend', htmlString);
  22.         });
  23.     });
  24.  
  25.     buyButton.addEventListener('click', () => {
  26.         let [products, prices, decFactors] = [[], [], []];
  27.         // взимане и итериране на html elements
  28.         [...document.querySelectorAll('tbody tr')].forEach(tr => {
  29.             if (tr.querySelector('input').checked) {
  30.                 let data = tr.querySelectorAll('p');
  31.                 products.push(data[0].textContent);
  32.                 prices.push(Number(data[1].textContent));
  33.                 decFactors.push(Number(data[2].textContent));
  34.             }
  35.         });
  36.         // взимане на стойности с амумолатор
  37.         let totalPrice = prices.reduce((acc, curr) => acc + curr);
  38.         let avgFactor = decFactors.reduce((sum, num) => (sum + num) / decFactors.length)
  39.         textArea2.value = `Bought furniture: ${products.join(', ')}\nTotal price: ${totalPrice.toFixed(2)}\nAverage decoration factor: ${avgFactor.toFixed(2)}`;
  40.     });
  41.    
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement