Advertisement
Sichanov

8. Furniture

Mar 25th, 2023
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     let buttons = document.querySelectorAll('button');
  3.     let generate = buttons[0];
  4.     let buy = buttons[1];
  5.     let tbody = document.querySelector('tbody');
  6.     let item = [];
  7.     generate.addEventListener('click', e => {
  8.         item = JSON.parse(document.querySelector('textarea').value);
  9.         let tr = document.createElement('tr');
  10.         tbody.appendChild(tr);
  11.         let tdImg = document.createElement('td');
  12.         tr.appendChild(tdImg);
  13.         let tdImgImg = document.createElement('img');
  14.         tdImg.appendChild(tdImgImg);
  15.         tdImgImg.setAttribute('src', `${item[0].img}`);
  16.         let tdName = document.createElement('td');
  17.         tr.appendChild(tdName);
  18.         let tdNameName = document.createElement('p');
  19.         tdNameName.textContent = `${item[0].name}`;
  20.         tdName.appendChild(tdNameName);
  21.         let tdPrice = document.createElement('td');
  22.         tr.appendChild(tdPrice);
  23.         let tdPricePrice = document.createElement('p');
  24.         tdPrice.appendChild((tdPricePrice));
  25.         tdPrice.textContent = `${item[0].price}`;
  26.         let tdDecoration = document.createElement('td');
  27.         tr.appendChild(tdDecoration);
  28.         let tdDecorationDecoration = document.createElement('p');
  29.         tdDecoration.appendChild(tdDecorationDecoration);
  30.         tdDecoration.textContent = `${item[0].decFactor}`;
  31.         let tdInput = document.createElement('td');
  32.         tr.appendChild(tdInput);
  33.         let tdInputInput = document.createElement('input');
  34.         tdInputInput.setAttribute('type', 'checkbox');
  35.         tdInput.appendChild(tdInputInput);
  36.     });
  37.     let boughtItems = [];
  38.     let totalPrice = 0;
  39.     let averageDecoration = [];
  40.     buy.addEventListener('click', e => {
  41.         let allTrs = Array.from(document.querySelectorAll('tr:nth-child(n+2)'));
  42.         for (const tr of allTrs) {
  43.             let check = tr.lastChild.querySelector('input[type="checkbox"]');
  44.             if (check.checked) {
  45.                 let name = tr.querySelector('td:nth-child(2)').textContent;
  46.                 let price = Number(tr.querySelector('td:nth-child(3)').textContent);
  47.                 let decoration = Number(tr.querySelector('td:nth-child(4)').textContent);
  48.                 boughtItems.push(name);
  49.                 totalPrice += price;
  50.                 averageDecoration.push(decoration);
  51.  
  52.             }
  53.         }
  54.         let textArea = document.querySelector('#exercise > textarea:nth-child(5)');
  55.         textArea.value = `Bought furniture: ${boughtItems.join(', ')}` + '\n' +
  56.             `Total price: ${totalPrice.toFixed(2)}` + '\n' +
  57.             `Average decoration factor: ${averageDecoration.reduce((a, b) => a + b, 0) / averageDecoration.length}`;
  58.     });
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement