Advertisement
RRPetrova

Furn

Jan 29th, 2021 (edited)
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. function solve() {
  2. let bts = Array.from(document.querySelectorAll("button"));
  3. bts.forEach(b => b.addEventListener("click", clicked));
  4.  
  5. let boughtField = document.querySelectorAll("textarea")[1];
  6.  
  7. function clicked(ev) {
  8. if (ev.target.textContent == "Generate") {
  9. let inputField = document.querySelectorAll("textarea")[0];
  10. let parsed = JSON.parse(inputField.value);
  11.  
  12. parsed.forEach(pr => {
  13. let { name, img, price, decFactor } = pr;
  14. let htmlStr = `<tr>
  15. <td><img src="${img}"></td>
  16. <td><p>${name}</p></td>
  17. <td><p>${price}</p></td>
  18. <td><p>${decFactor}</p></td>
  19. <td><input type="checkbox" /></td>
  20. </tr>`
  21.  
  22. document.querySelector("tbody").innerHTML += htmlStr;
  23. });
  24. inputField.value = "";
  25.  
  26. }
  27. if (ev.target.textContent == "Buy") {
  28. let boxes = document.querySelectorAll('input[type=checkbox]');
  29.  
  30. let boughtProdName = [];
  31. let boughtProdPrice = [];
  32. let boughtProdFact = [];
  33. boxes.forEach(ch => {
  34. // console.log(ch.checked)
  35. if (ch.checked === true) {
  36. let rowChecked = ch.parentNode.parentNode;
  37. let data = rowChecked.querySelectorAll("p");
  38. boughtProdName.push(data[0].textContent);
  39. boughtProdPrice.push(Number(data[1].textContent));
  40. boughtProdFact.push(Number(data[2].textContent));
  41. }
  42. });
  43. let avg = boughtProdFact.reduce((a, b) => a + b,0) / boughtProdFact.length;
  44. let tot = boughtProdPrice.reduce((a, b) => a += b);
  45. let res = `Bought furniture: ${boughtProdName.join(", ")}\n`;
  46. res += `Total price: ${tot.toFixed(2)}\n`;
  47. res += `Average decoration factor: ${avg}`;
  48. boughtField.textContent = res;
  49.  
  50. }
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement