Advertisement
Onesible

Untitled

Mar 27th, 2025
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     let tableBody = document.querySelector('tbody');
  3.     let furnitures = document.querySelector('#input textarea');
  4.     document.querySelector('#input input').addEventListener('click', generate);
  5.    
  6.     function createCell(text) {
  7.         let td = document.createElement('td');
  8.         let p = document.createElement('p');
  9.         p.textContent = text;
  10.         td.appendChild(p);
  11.         return td;
  12.     }
  13.  
  14.     function generate(e) {
  15.         e.preventDefault();
  16.  
  17.         for (let item of JSON.parse(furnitures.value)) {
  18.             let tr = document.createElement('tr');
  19.  
  20.             let imgData = document.createElement('td');
  21.             let img = document.createElement('img');
  22.             img.src = item.img;
  23.             imgData.appendChild(img);
  24.             tr.appendChild(imgData);
  25.  
  26.             tr.appendChild(createCell(item.name));
  27.             tr.appendChild(createCell(item.price));
  28.             tr.appendChild(createCell(item.decFactor));
  29.  
  30.             let checkCell = document.createElement('td');
  31.             let checkbox = document.createElement('input')
  32.             checkbox.type = 'checkbox';
  33.             checkCell.appendChild(checkbox);
  34.             tr.appendChild(checkCell);
  35.  
  36.             tableBody.appendChild(tr);
  37.         }
  38.     }
  39.  
  40.     document.querySelector('input[value="Buy"]').addEventListener('click', buyItems);
  41.  
  42.     function buyItems(e) {
  43.         e.preventDefault();
  44.  
  45.         let checkedItems = document.querySelectorAll('input[type="checkbox"]:checked');
  46.        
  47.         if (checkedItems.length === 0) {
  48.             return;
  49.         }
  50.  
  51.         let itemNames = [];
  52.         let totalPrice = 0;
  53.         let avgDecFactor = 0;
  54.  
  55.         for (let item of checkedItems) {
  56.             let row = item.parentElement.parentElement;
  57.            
  58.             itemNames.push(row.querySelector('td:nth-child(2)').textContent);
  59.             totalPrice += Number(row.querySelector('td:nth-child(3)').textContent);
  60.             avgDecFactor += Number(row.querySelector('td:nth-child(4)').textContent);
  61.         }
  62.  
  63.         let result = `Bought furniture: ${itemNames.join(', ')}\n`;
  64.         result += `Total price: ${totalPrice}\n`;
  65.         result += `Average decoration factor: ${avgDecFactor / checkedItems.length}`;
  66.  
  67.         document.querySelector('textarea[rows="4"]').value = result;
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement