Advertisement
milovan7

Furniture / JS Advanced

Apr 12th, 2021
1,301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. function solve() {
  2. let textarea = document.querySelectorAll('textarea');
  3. let tbody = document.querySelector('tbody');
  4.  
  5. [...document.querySelectorAll('button')].forEach(btn => btn.addEventListener('click', execute));
  6. function execute(btn) {
  7. if (!textarea[0].value) return;
  8. if (btn.target.textContent === 'Generate') {
  9. let input = JSON.parse(textarea[0].value);
  10. input.forEach(furniture => {
  11. tbody.innerHTML += `<tr>
  12. <td><img src=${furniture.img}></td>
  13. <td><p>${furniture.name}</p></td>
  14. <td><p>${furniture.price}</p></td>
  15. <td><p>${furniture.decFactor}</p></td>
  16. <td><input type="checkbox"/></td>
  17. </tr>`
  18. })
  19. } else {
  20. let furnitureName = [];
  21. let totalPrice = 0;
  22. let averageDecFactor = 0;
  23. [...document.querySelectorAll('input:checked')]
  24. .forEach(furniture => {
  25. let parentRow = furniture.parentNode.parentNode;
  26. averageDecFactor += Number(parentRow.children[3].textContent);
  27. totalPrice += Number(parentRow.children[2].textContent);
  28. furnitureName.push(parentRow.children[1].textContent);
  29. });
  30. textarea[1].textContent += `Bought furniture: ${furnitureName.join(', ')}\n`;
  31. textarea[1].textContent += `Total price: ${totalPrice.toFixed(2)}\n`;
  32. textarea[1].textContent += `Average decoration factor: ${averageDecFactor / furnitureName.length}`;
  33. }
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement