Advertisement
GeorgiLukanov87

08. Furniture

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