Advertisement
Guest User

Untitled

a guest
Jun 11th, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. function solve() {
  2. const tBody = document.getElementsByTagName("tbody")[0];
  3. const textInput = document.getElementsByTagName("textarea")[0];
  4. const generateBtn = document.getElementsByTagName("button")[0];
  5. const buyBtn = document.getElementsByTagName("button")[1];
  6. const resultOutput = document.getElementsByTagName("textarea")[1];
  7.  
  8. generateBtn.addEventListener("click", function () {
  9. let furnitureArray = JSON.parse(textInput.value);
  10. for (furniture of furnitureArray) {
  11. //Create tr
  12. const newTr = document.createElement("tr");
  13. //Create image td
  14. const newImageTd = document.createElement("td");
  15. const newImageImg = document.createElement("img");
  16. newImageImg.src = furniture["img"];
  17. newImageTd.appendChild(newImageImg);
  18. newTr.appendChild(newImageTd);
  19. //Create name td
  20. const newNameTd = document.createElement("td");
  21. const newNameP = document.createElement("p");
  22. newNameP.innerHTML = furniture["name"];
  23. newNameTd.appendChild(newNameP);
  24. newTr.appendChild(newNameTd);
  25. //Create price td
  26. const newPriceTd = document.createElement("td");
  27. const newPriceP = document.createElement("p");
  28. newPriceP.innerHTML = furniture["price"];
  29. newPriceTd.appendChild(newPriceP);
  30. newTr.appendChild(newPriceTd);
  31. //Create decoration td
  32. const newDecorationTd = document.createElement("td");
  33. const newDecorationP = document.createElement("p");
  34. newDecorationP.innerHTML = furniture["decFactor"];
  35. newDecorationTd.appendChild(newDecorationP);
  36. newTr.appendChild(newDecorationTd);
  37. //Create checkbox td
  38. const newCheckboxTd = document.createElement("td");
  39. const newCheckbox = document.createElement("input");
  40. newCheckbox.type = "checkbox";
  41. newCheckboxTd.appendChild(newCheckbox);
  42. newTr.appendChild(newCheckboxTd);
  43. //Append tr to the table
  44. tBody.appendChild(newTr);
  45. }
  46. });
  47.  
  48. buyBtn.addEventListener("click", function () {
  49. const trs = document.querySelectorAll('tbody tr');
  50. let totalFurniture = {
  51. furniture: "",
  52. totalPrice: 0,
  53. totalDecFactor: 0,
  54. }
  55. for (i = 0; i < trs.length; ++i) {
  56. if (trs[i].getElementsByTagName("td")[4].getElementsByTagName("input")[0].checked) {
  57. totalFurniture["furniture"] += `${trs[i].getElementsByTagName("td")[1].
  58. getElementsByTagName("p")[0].innerHTML}, `
  59. totalFurniture["totalPrice"] += Number(trs[i].getElementsByTagName("td")[2].
  60. getElementsByTagName("p")[0].innerHTML)
  61. totalFurniture["totalDecFactor"] += Number(trs[i].getElementsByTagName("td")[3].
  62. getElementsByTagName("p")[0].innerHTML)
  63. }
  64. }
  65. if (totalFurniture["furniture"] !== "") {
  66. resultOutput.value = `Bought furniture: ${totalFurniture["furniture"].
  67. substring(0, totalFurniture["furniture"].
  68. length - 2)}\nTotal price: ${totalFurniture["totalPrice"].toFixed(2)}\nAverage decoration factor: ${totalFurniture["totalDecFactor"]}`;
  69. }
  70. });
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement