Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve() {
- // деструкториране на querySelector
- let [generateButton, buyButton] = [...document.querySelectorAll('button')];
- let [textArea1, textArea2] = [...document.querySelectorAll('textarea')];
- generateButton.addEventListener('click', () => {
- let products = JSON.parse(textArea1.value);
- products.forEach(o => {
- // деструкториране на обект
- let {img, name, price, decFactor} = o;
- // създаване на htmlString
- let htmlString = `<tr><td>
- <img src="${img}">
- </td><td><p>${name}</p>
- <td><p>${Number(price)}</p></td>
- <td><p>${Number(decFactor)}</p></td>
- <td><input type="checkbox"/></td>
- </tr>`;
- // добавяне на htmlString към html-а на страницата
- let tBody = document.querySelector('tbody');
- tBody.insertAdjacentHTML('beforeend', htmlString);
- });
- });
- buyButton.addEventListener('click', () => {
- let [products, prices, decFactors] = [[], [], []];
- // взимане и итериране на html elements
- [...document.querySelectorAll('tbody tr')].forEach(tr => {
- if (tr.querySelector('input').checked) {
- let data = tr.querySelectorAll('p');
- products.push(data[0].textContent);
- prices.push(Number(data[1].textContent));
- decFactors.push(Number(data[2].textContent));
- }
- });
- // взимане на стойности с амумолатор
- let totalPrice = prices.reduce((acc, curr) => acc + curr);
- let avgFactor = decFactors.reduce((sum, num) => (sum + num) / decFactors.length)
- textArea2.value = `Bought furniture: ${products.join(', ')}\nTotal price: ${totalPrice.toFixed(2)}\nAverage decoration factor: ${avgFactor.toFixed(2)}`;
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement