SHARE
TWEET

Untitled

a guest May 22nd, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.   let textareaInputElement = document.getElementsByTagName('textarea')[0];
  3.   let textareaOutputElement = document.getElementsByTagName('textarea')[1];
  4.   let generateButtonElement = document.getElementsByTagName('button')[0];
  5.   let buyButtonElement = document.getElementsByTagName('button')[1];
  6.   let tableElement = document.getElementsByTagName('tbody')[0];
  7.   let products = {};
  8.  
  9.   generateButtonElement.addEventListener('click', () => {
  10.     let newFurnitureJson = textareaInputElement.value;
  11.     let newFurnitureObj = JSON.parse(newFurnitureJson);
  12.     products[newFurnitureObj.name] = newFurnitureObj;
  13.     let trElement = document.createElement('tr');
  14.     for (let i = 0; i < 5; i++) {
  15.     let tdElement = document.createElement('td');
  16.       switch(i) {
  17.         case 0: {
  18.           let element = document.createElement('img');
  19.           let src = "";
  20.           src += newFurnitureObj.img;
  21.           element.src = src;
  22.           tdElement.appendChild(element);
  23.           break;
  24.         }
  25.         case 1: {
  26.           let element = document.createElement('p');
  27.           element.textContent = newFurnitureObj.name;
  28.           tdElement.appendChild(element);
  29.           break;
  30.         }
  31.         case 2: {
  32.           let element = document.createElement('p');
  33.           element.textContent = newFurnitureObj.price;
  34.           tdElement.appendChild(element);
  35.           break;
  36.         }  
  37.         case 3: {
  38.           let element = document.createElement('p');
  39.           element.textContent = newFurnitureObj.decFactor;
  40.           tdElement.appendChild(element);
  41.           break;
  42.         }  
  43.         case 4: {
  44.           let element = document.createElement('input');
  45.           products[newFurnitureObj.name].checkbox = element;
  46.           element.type = 'checkbox';
  47.           tdElement.appendChild(element);
  48.           break;
  49.         }        
  50.       }
  51.      
  52.       trElement.appendChild(tdElement);
  53.     }
  54.    
  55.     console.log(trElement.children[0]);
  56.     tableElement.appendChild(trElement);
  57.   })
  58.  
  59.   buyButtonElement.addEventListener('click', () => {
  60.     let boughtProducts = {};
  61.     let keys = Object.keys(products);
  62.     for (let i = 0; i < keys.length; i++) {
  63.       const element = products[keys[i]];
  64.       if (element.checkbox.checked) {
  65.         boughtProducts[element.name] = element;
  66.       }
  67.     }
  68.  
  69.     let list = '';
  70.     let boughtKeys = Object.keys(boughtProducts);
  71.     let totalPrice = 0;
  72.     let decFactors = 0;
  73.     for (let i = 0; i < boughtKeys.length; i++) {
  74.       const element = boughtProducts[boughtKeys[i]];
  75.       if (i == boughtKeys.length - 1) {
  76.         list += element.name;
  77.       } else {
  78.         list += element.name + ', ';
  79.       }
  80.  
  81.       totalPrice += Number(element.price);
  82.       decFactors += Number(element.decFactor);
  83.     }
  84.  
  85.     textareaOutputElement.textContent += `Bought furniture: ${list}\n`;
  86.     textareaOutputElement.textContent += `Total price: ${totalPrice.toFixed(2)}\n`;
  87.     textareaOutputElement.textContent += `Average decoration factor: ${decFactors / boughtKeys.length}`;
  88.  
  89.   })
  90. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top