Guest User

Untitled

a guest
May 8th, 2023
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. function solve() {
  2. const inputElement = document.querySelectorAll('textarea')[0];
  3. const outputElement = document.querySelectorAll('textarea')[1];
  4.  
  5. const generateButton = document.querySelectorAll('button')[0];
  6. const buyButton = document.querySelectorAll('button')[1];
  7.  
  8. const tBodyElement = document.querySelector('tbody');
  9.  
  10. generateButton.addEventListener('click', appendingItems);
  11. // img, name, price, points
  12. function appendingItems() {
  13. const addedItems = JSON.parse(inputElement.value);
  14. addedItems.forEach(item => {
  15. const itemInformation = Array.from(Object.entries(item));
  16.  
  17. const imgKVP = itemInformation[1];
  18. const nameKVP = itemInformation[0];
  19. const priceKVP = itemInformation[2];
  20. const pointsKVP = itemInformation[3];
  21.  
  22. const trElement = document.createElement('tr');
  23.  
  24. for (let i = 0; i < itemInformation.length; i++) {
  25. const tdElement = document.createElement('td');
  26.  
  27. //appending img element
  28. if (i === 0) {
  29. const imgElement = document.createElement('img');
  30. imgElement.setAttribute('src', imgKVP[1]);
  31. tdElement.appendChild(imgElement);
  32. } else if (i === 1) {
  33.  
  34. //appending p element with item name text content
  35. const pElement = document.createElement('p');
  36. pElement.textContent = nameKVP[1];
  37. tdElement.appendChild(pElement);
  38. } else if (i === 2) {
  39.  
  40. //appending p element with item price text content
  41. const pElement = document.createElement('p');
  42. pElement.textContent = priceKVP[1];
  43. tdElement.appendChild(pElement);
  44. } else if (i === 3) {
  45.  
  46. //appending p element with item decoration points text content
  47. const pElement = document.createElement('p');
  48. pElement.textContent = pointsKVP[1];
  49. tdElement.appendChild(pElement);
  50. trElement.appendChild(tdElement);
  51.  
  52. const tdCheckbox = document.createElement('td');
  53. //appending input element
  54. const checkbox = document.createElement('input');
  55. checkbox.setAttribute('type', 'checkbox');
  56. tdCheckbox.appendChild(checkbox);
  57.  
  58. trElement.appendChild(tdCheckbox);
  59. break;
  60. }
  61. trElement.appendChild(tdElement);
  62. }
  63. tBodyElement.appendChild(trElement);
  64. });
  65. }
  66.  
  67. buyButton.addEventListener('click', purchaseFinishing);
  68.  
  69. function purchaseFinishing() {
  70. const purchasedFurniture = [];
  71. let totalPrice = 0;
  72. const decorationPoints = [];
  73. const markedRows = [];
  74.  
  75. //sorting all the marked rows
  76. const rowElements = document.querySelectorAll('tbody tr');
  77. rowElements.forEach(row => {
  78. const inputElement = row.querySelector('input');
  79. if (inputElement.checked) {
  80. markedRows.push(row);
  81. }
  82. });
  83.  
  84. debugger;
  85. markedRows.forEach(row => {
  86. const itemNameElement = row.querySelectorAll('td p')[0];
  87. purchasedFurniture.push(itemNameElement.textContent);
  88.  
  89. const itemPriceElement = row.querySelectorAll('td p')[1];
  90. totalPrice += Number(itemPriceElement.textContent)
  91.  
  92. const decorationPointsElement = row.querySelectorAll('td p')[2];
  93. decorationPoints.push(Number(decorationPointsElement.textContent));
  94. });
  95. const averageDecorationPoints = decorationPoints.reduce((a, b) => a + b, 0) / decorationPoints.length;
  96.  
  97. outputElement.value = `Bought furniture: ${purchasedFurniture.join(', ')}\nTotal price: ${totalPrice.toFixed(2)}\nAverage decoration factor: ${averageDecorationPoints}`;
  98.  
  99. }
  100. }
Add Comment
Please, Sign In to add comment