Advertisement
elena1234

Generate table from JSON ; dissable button; JavaScript

Nov 7th, 2021
1,061
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     let inputArea = document.querySelectorAll('textarea')[0];
  3.     let generateButton = document.querySelectorAll('button')[0];
  4.     let buyButton = document.querySelectorAll('button')[1];
  5.     let table = document.querySelector('.table');
  6.  
  7.     generateButton.addEventListener('click', generateTable);
  8.     buyButton.addEventListener('click', buyFurniture);
  9.    
  10.  
  11.     function generateTable() {
  12.         let furnitures = JSON.parse(inputArea.value);
  13.         furnitures.forEach(currentFurniture => {
  14.             let values = Object.values(currentFurniture);
  15.             for (let i = 0; i < values.length; i += 4) {
  16.                 let link = values[i];
  17.                 let furniture = values[i + 1];
  18.                 let price = values[i + 2];
  19.                 let decFactor = values[i + 3];
  20.                 var checkBox = ('<input type="checkbox"/>');
  21.  
  22.                 let newRow = table.insertRow(-1);
  23.                 let cell1 = newRow.insertCell(0);
  24.                 let cell2 = newRow.insertCell(1);
  25.                 let cell3 = newRow.insertCell(2);
  26.                 let cell4 = newRow.insertCell(3);
  27.                 let cell5 = newRow.insertCell(4);
  28.  
  29.                 cell1.innerHTML = `<img src =\"${link}">`;
  30.                 cell2.innerHTML = furniture;
  31.                 cell3.innerHTML = price;
  32.                 cell4.innerHTML = decFactor;
  33.                 cell5.innerHTML = checkBox;
  34.             }
  35.  
  36.             inputArea.value = '';
  37.         })
  38.     }
  39.  
  40.     function buyFurniture () {
  41.         let allCheckBoxes = document.querySelectorAll('input');
  42.         let allOrderedFurnitures = [];
  43.         for (const box of allCheckBoxes) {
  44.             if (box.checked) {
  45.                 let tr = box.parentElement.parentElement;
  46.                 let children = tr.childNodes;
  47.                 let furnitureName = children[1].innerHTML;
  48.                 let price = children[2].innerHTML;
  49.                 let decFactor = children[3].innerHTML;
  50.  
  51.                 let furniture = {};
  52.                 furniture.name = furnitureName;
  53.                 furniture.price = Number(price);
  54.                 furniture.decFactor = Number(decFactor);
  55.                 allOrderedFurnitures.push(furniture);
  56.             }
  57.         }
  58.  
  59.         calculateOutput(allOrderedFurnitures);
  60.     }
  61.  
  62.  
  63.     function calculateOutput(allOrderedFurnitures) {
  64.                 let outputArea = document.querySelectorAll('textarea')[1];
  65.                 let boughtFurniture = [];
  66.                 let totalPrice = 0;
  67.                 let allDecFactors = [];
  68.                 for (const furniture of allOrderedFurnitures) {
  69.                     boughtFurniture.push(furniture.name);
  70.                     totalPrice += Number(furniture.price);
  71.                     allDecFactors.push(Number(furniture.decFactor));
  72.                 }
  73.  
  74.                 let total = allDecFactors.reduce((a, b) => a + b, 0);
  75.                 let averageDecFactor = total / allDecFactors.length;
  76.                 outputArea.value += `Bought furniture: ${boughtFurniture.join(', ')} \n`;
  77.                 outputArea.value += `Total price: ${totalPrice.toFixed(2)} \n`;
  78.                 outputArea.value += `Average decorator factor: ${averageDecFactor} \n`;
  79.                 buyButton.disabled = true;
  80.             }
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement