Advertisement
Guest User

Untitled

a guest
Oct 16th, 2021
211
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('img');
  31.         img.src = item.img;
  32.         console.log(img);
  33.         imgCell.appendChild(img);
  34.  
  35.         const nameP = document.createElement('p');
  36.         nameP.textContent = item.name;
  37.         nameCell.appendChild(nameP);
  38.        
  39.         const decP = document.createElement('p');
  40.         decP.textContent = item.decFactor;
  41.         console.log(decP);
  42.         decFactorCell.appendChild(decP);
  43.        
  44.         const priceP = document.createElement('p');
  45.         priceP.textContent = item.price;
  46.         priceCell.appendChild(priceP);
  47.        
  48.         const check = document.createElement('input');
  49.         check.type = 'checkbox';
  50.         checkCell.appendChild(check);
  51.  
  52.         row.appendChild(imgCell);
  53.         row.appendChild(nameCell);
  54.         row.appendChild(priceCell);
  55.         row.appendChild(decFactorCell);
  56.         row.appendChild(checkCell);
  57.  
  58.         table.appendChild(row);
  59.  
  60.         }
  61.     }
  62.  
  63.     /* # table generation */
  64.     // read input JSON and parse it
  65.     // for every array element, create table row
  66.    
  67.  
  68.  
  69.     /* # buy furniture */  
  70.     function buy(e) {
  71.         const furniture = Array
  72.         .from(document.querySelector('input[type="checkbox"]:checked'))
  73.         .map(b => b.parentElement.parentElement)
  74.         .map(r => ({
  75.             name: r.children[1].textContent,
  76.             price: Number(r.children[2].textContent),
  77.             decFactor: Number(r.children[3].textContent)
  78.         }));
  79.        
  80.         let total = 0;
  81.         let decFactor = 0;
  82.  
  83.         for (let item of furniture) {
  84.             names.push(item.name);
  85.             total += item.price;
  86.             decFactor += item.decFactor;
  87.         }
  88.         const result = `Bought furniture: ${names.join(', ')}
  89.     Total price: ${total.toFixed(2)}
  90.     Average decoration factor: ${decFactor / furniture.length}`;
  91.  
  92.     output.value = result;
  93.     }  
  94.     // select all checkboxes
  95.     // filter only checked checkboxes
  96.     // repeat for every selected checkbox
  97.     // -- select parent row
  98.     // -- read item information
  99.     // display output
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement