Sichanov

03. Car Dealers

Apr 8th, 2023 (edited)
709
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     const make = document.getElementById('make');
  3.     const model = document.getElementById('model');
  4.     const year = document.getElementById('year');
  5.     const fuel = document.getElementById('fuel');
  6.     const originalCost = document.getElementById('original-cost');
  7.     const sellingPrice = document.getElementById('selling-price');
  8.     const publishBtn = document.getElementById('publish');
  9.     const tbody = document.getElementById('table-body');
  10.     const carList = document.getElementById('cars-list');
  11.     const profitValue = document.getElementById('profit');
  12.     const form = document.getElementsByTagName('form')[0];
  13.     let profit = 0;
  14.  
  15.     make.value = 'BMW';
  16.     model.value = 'Z3';
  17.     year.value = '2022';
  18.     fuel.value = 'diesel';
  19.     originalCost.value = 52300;
  20.     sellingPrice.value = 132300;
  21.  
  22.     publishBtn.addEventListener('click', publishHandler);
  23.  
  24.     function publishHandler(e) {
  25.         e.preventDefault();
  26.  
  27.         if ((make.value !== '') &&
  28.             (model.value !== '') &&
  29.             (year.value !== '') &&
  30.             (sellingPrice.value !== '') &&
  31.             (originalCost.value !== '') &&
  32.             (Number(sellingPrice.value) > Number(originalCost.value))) {
  33.             const tr = createElement('tr', '', tbody, '', ['row']);
  34.             const brandCar = createElement('td', make.value, tr, 'make-value');
  35.             const modelCar = createElement('td', model.value, tr, 'model-value');
  36.             const yearCar = createElement('td', year.value, tr, 'year-value');
  37.             const fuelCar = createElement('td', fuel.value, tr, 'fuel-value');
  38.             const originalCostCar = createElement('td', originalCost.value, tr, 'original-cost-value');
  39.             const sellingPriceCar = createElement('td', sellingPrice.value, tr, 'selling-price-value');
  40.             const tdButtons = createElement('td', '', tr);
  41.             const editBtn = createElement('button', 'Edit', tdButtons, '', ['action-btn', 'edit']);
  42.             const sellBtn = createElement('button', 'Sell', tdButtons, '', ['action-btn', 'sell']);
  43.             make.value = '';
  44.             model.value = '';
  45.             year.value = '';
  46.             fuel.value = '';
  47.             originalCost.value = '';
  48.             sellingPrice.value = '';
  49.  
  50.             editBtn.addEventListener('click', editHandler);
  51.             sellBtn.addEventListener('click', sellHandler);
  52.         }
  53.     }
  54.  
  55.     function sellHandler(e) {
  56.         const element = e.currentTarget.parentNode.parentNode
  57.         const elementChildren = e.currentTarget.parentNode.parentNode.children;
  58.         // const elementChildren = this.parentNode.parentNode.children;
  59.         console.log(elementChildren);
  60.         const makeModel = `${elementChildren[0].textContent} ${elementChildren[1].textContent}`;
  61.         const yearCar = elementChildren[2].textContent;
  62.         const priceDiff = elementChildren[5].textContent - elementChildren[4].textContent;
  63.         profit += priceDiff;
  64.         const li = createElement('li', '', carList, '', ['each-list']);
  65.         createElement('span', makeModel, li);
  66.         createElement('span', yearCar, li);
  67.         createElement('span', priceDiff, li);
  68.         profitValue.textContent = profit.toFixed(2);
  69.         element.remove()
  70.     }
  71.  
  72.     function editHandler() {
  73.         const element = this.parentNode.parentNode;
  74.         console.log(element.children);
  75.         make.value = element.children[0].textContent;
  76.         model.value = element.children[1].textContent;
  77.         year.value = element.children[2].textContent;
  78.         fuel.value = element.children[3].textContent;
  79.         originalCost.value = element.children[4].textContent;
  80.         sellingPrice.value = element.children[5].textContent;
  81.         tbody.innerHTML = '';
  82.  
  83.     }
  84.  
  85.     function createElement(type, content, parentNode, id, classes, attributes) {
  86.         const htmlElement = document.createElement(type);
  87.  
  88.         if (content && type !== 'input') {
  89.             htmlElement.textContent = content;
  90.         }
  91.  
  92.         if (content && type === 'input') {
  93.             htmlElement.value = content;
  94.         }
  95.  
  96.         if (id) {
  97.             htmlElement.id = id;
  98.         }
  99.  
  100.         if (classes) {
  101.             htmlElement.classList.add(...classes);
  102.         }
  103.  
  104.         if (parentNode) {
  105.             parentNode.appendChild(htmlElement);
  106.         }
  107.  
  108.         if (attributes) {
  109.             for (const key in attributes) {
  110.                 htmlElement.setAttribute(key, attributes[key]);
  111.             }
  112.         }
  113.  
  114.         return htmlElement;
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment