Liliana797979

furniture - js advanced

Oct 16th, 2021
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     /* # configure event listeners # */
  3.     // select all buttons
  4.     // first button -> table generation
  5.     // second button -> buy furniture
  6.     const table = document.querySelector('table.table tbody');
  7.     const [input, output] = Array.from(document.querySelectorAll('textarea'));
  8.     //console.log(input, output);
  9.  
  10.     const [generateBtn, buyBtn] = Array.from(document.querySelectorAll('button', buy));
  11.     //console.log(generateBtn, buyBtn);
  12.  
  13.     generateBtn.addEventListener('click', generate);
  14.     buyBtn.addEventListener('click', buy);
  15.  
  16.     function generate(e) {
  17.         const data = JSON.parse(input.value);
  18.         //console.log(data);
  19.             for (let item of data) {
  20.                
  21.         const row = document.createElement('tr');
  22.  
  23.         const imgCell = document.createElement('td');
  24.         const nameCell = document.createElement('td');
  25.         const priceCell = document.createElement('td');
  26.         const decFactorCell = document.createElement('td');
  27.         const imgTd = document.createElement('td');
  28.         const checkCell = document.createElement('td');
  29.  
  30.         const img = document.createElement('td');
  31.         img.src = item.img;
  32.         imgCell.appendChild(img);
  33.  
  34.         const nameP = document.createElement('p');
  35.         nameP.textContent = item.name;
  36.         nameCell.appendChild(nameP);
  37.        
  38.         const priceP = document.createElement('p');
  39.         priceP.textContent = item.decFactor;
  40.         decFactorCell.appendChild(decP);
  41.        
  42.         const check = document.createElement('input');
  43.         check.type = 'checkbox';
  44.         checkCell.appendChild(check);
  45.  
  46.         row.appendChild(imgCell);
  47.         row.appendChild(nameCell);
  48.         row.appendChild(priceCell);
  49.         row.appendChild(decFactorCell);
  50.         row.appendChild(checkCell);
  51.  
  52.         table.appendChild(row);
  53.  
  54.         }
  55.     }
  56.  
  57.     /* # table generation */
  58.     // read input JSON and parse it
  59.     // for every array element, create table row
  60.    
  61.  
  62.  
  63.     /* # buy furniture */  
  64.     function buy(e) {
  65.         const furniture = Array
  66.         .from(document.querySelector('input[type="checkbox"]:checked'))
  67.         .map(b => b.parentElement.parentElement)
  68.         .map(r => ({
  69.             name: r.children[1].textContent,
  70.             price: Number(r.children[2].textContent),
  71.             decFactor: Number(r.children[3].textContent)
  72.         }));
  73.        
  74.         let total = 0;
  75.         let decFactor = 0;
  76.  
  77.         for (let item of furniture) {
  78.             names.push(item.name);
  79.             total += item.price;
  80.             decFactor += item.decFactor;
  81.         }
  82.         const result = `Bought furniture: ${names.join(', ')}
  83.     Total price: ${total.toFixed(2)}
  84.     Average decoration factor: ${decFactor / furniture.length}`;
  85.  
  86.     output.value = result;
  87.     }  
  88.     // select all checkboxes
  89.     // filter only checked checkboxes
  90.     // repeat for every selected checkbox
  91.     // -- select parent row
  92.     // -- read item information
  93.     // display output
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment