Advertisement
EntropyStarRover

Exercise: DOM 8. Furniture

Oct 9th, 2019
192
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.   let btns=Array.from(document.getElementsByTagName("button"));
  3.   let btnGenerate=btns[0];
  4.   let btnBuy=btns[1];
  5.  
  6.   let inputField=document.getElementsByTagName("textarea")[0];
  7.   let boughtField=document.getElementsByTagName("textarea")[1];
  8.   let tbl=document.getElementsByTagName("table")[0];
  9.  
  10.   btnGenerate.addEventListener("click", function(){
  11.     let newProductArr=JSON.parse(inputField.value);
  12.    
  13.     newProductArr.forEach(prod => {
  14.       let newRow=document.createElement("tr");
  15.  
  16.       let imgTd=document.createElement("td");
  17.       let im=document.createElement("img");
  18.       im.src=prod["img"];
  19.       imgTd.appendChild(im);
  20.  
  21.       let nameTd=document.createElement("td");
  22.       nameTd.textContent=prod["name"];;
  23.  
  24.       let priceTd=document.createElement("td");
  25.       priceTd.textContent=prod["price"];;
  26.  
  27.       let decorationTd=document.createElement("td");
  28.       decorationTd.textContent=prod["decFactor"];
  29.  
  30.  
  31.       let markTd = document.createElement('input');
  32.       markTd.type = "checkbox";
  33.       markTd.className="chbk";
  34.        
  35.       newRow.appendChild(imgTd);
  36.       newRow.appendChild(nameTd);
  37.       newRow.appendChild(priceTd);
  38.       newRow.appendChild(decorationTd);
  39.       newRow.appendChild(markTd);
  40.       tbl.appendChild(newRow);      
  41.     });
  42.   })
  43.  
  44.  btnBuy.addEventListener("click", function(){
  45. let total=0;
  46. let furnitureBought=[];
  47. let totaldecFr=0;
  48. let chbks=Array.from(document.getElementsByClassName("chbk"));
  49.  
  50. chbks.forEach(element => {
  51.   if(element.checked){
  52.     furnitureBought.push(element.parentNode.children[1].innerText);
  53.     total+=Number(element.parentNode.children[2].innerText);
  54.     totaldecFr+=Number(element.parentNode.children[3].innerText)
  55.   }
  56. });
  57.  
  58.     boughtField.value=`Bought furniture: ${furnitureBought.join(", ")}\nTotal price: ${total}\nAverage decoration factor: ${(totaldecFr/furnitureBought.length).toFixed(2)}`
  59.   });
  60.  
  61.  
  62. }
Advertisement
RAW Paste Data Copied
Advertisement