Advertisement
GeorgiLukanov87

01. Car Dealers - JS Advanced Exam - 25 Jun 2022

Mar 21st, 2023 (edited)
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // JS Advanced Exam - 25 Jun 2022
  2. // 01. Car Dealers
  3. // https://judge.softuni.org/Contests/Practice/Index/3519#0
  4.  
  5. function solve() {
  6.   const makeElement = document.getElementById('make');
  7.   const modelElement = document.getElementById('model');
  8.   const fuelElement = document.getElementById('fuel');
  9.   const yearElement = document.getElementById('year');
  10.   const originalCostElement = document.getElementById('original-cost');
  11.   const sellingPriceElement = document.getElementById('selling-price');
  12.  
  13.   const publishBtn = document.getElementById('publish');
  14.   const tableBodyElement = document.getElementById('table-body');
  15.   const carListElement = document.getElementById('cars-list');
  16.   const profitElement = document.getElementById('profit');
  17.  
  18.   let CarDetails = {};
  19.   let totalProfit = 0;
  20.   publishBtn.addEventListener('click', publishOffer);
  21.  
  22.   function publishOffer(e) {
  23.     e.preventDefault();
  24.     CarDetails = {
  25.       make: makeElement.value,
  26.       model: modelElement.value,
  27.       year: yearElement.value,
  28.       fuel: fuelElement.value,
  29.       originalCost: Number(originalCostElement.value),
  30.       sellingPrice: Number(sellingPriceElement.value),
  31.     }
  32.     if (!CarDetails.make ||
  33.       !CarDetails.model ||
  34.       !CarDetails.year ||
  35.       !CarDetails.fuel ||
  36.       !CarDetails.originalCost ||
  37.       !CarDetails.sellingPrice || CarDetails.originalCost >= CarDetails.sellingPrice) {
  38.       alert('Wrong input')
  39.       return;
  40.     }
  41.  
  42.     let trRow = document.createElement('tr');
  43.     trRow.classList.add('row');
  44.     customTd(CarDetails.make, trRow);
  45.     customTd(CarDetails.model, trRow);
  46.     customTd(CarDetails.year, trRow);
  47.     customTd(CarDetails.fuel, trRow);
  48.     customTd(CarDetails.originalCost, trRow);
  49.     customTd(CarDetails.sellingPrice, trRow);
  50.  
  51.     let tdBtnContainer = document.createElement('td');
  52.  
  53.     let editBtn = customBtn('Edit', tdBtnContainer, 'action-btn', 'edit');
  54.     editBtn.addEventListener('click', editHandler)
  55.     let sellBtn = customBtn('Sell', tdBtnContainer, 'action-btn', 'sell');
  56.     sellBtn.addEventListener('click', sellHandler)
  57.  
  58.     trRow.appendChild(tdBtnContainer);
  59.     tableBodyElement.appendChild(trRow);
  60.     clearInputs();
  61.   }
  62.  
  63.   function editHandler(e) {
  64.     let offerToEdit = e.target.parentNode.parentNode;
  65.     makeElement.value = offerToEdit.children[0].textContent;
  66.     modelElement.value = offerToEdit.children[1].textContent;
  67.     yearElement.value = offerToEdit.children[2].textContent;
  68.     fuelElement.value = offerToEdit.children[3].textContent;
  69.     originalCostElement.value = offerToEdit.children[4].textContent;
  70.     sellingPriceElement.value = offerToEdit.children[5].textContent;
  71.     offerToEdit.remove();
  72.   }
  73.  
  74.   function sellHandler(e) {
  75.     let offerToSell = e.target.parentNode.parentNode;
  76.  
  77.     let make = offerToSell.children[0].textContent;
  78.     let model = offerToSell.children[1].textContent;
  79.     let year = offerToSell.children[2].textContent;
  80.  
  81.     let originalPrice = offerToSell.children[4].textContent;
  82.     let sellPrice = offerToSell.children[5].textContent;
  83.     let sellDiff = Number(sellPrice) - Number(originalPrice);
  84.     totalProfit += sellDiff;
  85.  
  86.     let newLi = document.createElement('li');
  87.     newLi.classList.add('each-list');
  88.  
  89.     customSpan(make, model, newLi);
  90.     customSpan(year, '', newLi);
  91.     customSpan(sellDiff, '', newLi);
  92.     carListElement.appendChild(newLi);
  93.     profitElement.textContent = totalProfit.toFixed(2);
  94.     offerToSell.remove();
  95.   }
  96.  
  97.   function clearInputs() {
  98.     makeElement.value = '';
  99.     modelElement.value = '';
  100.     yearElement.value = '';
  101.     fuelElement.value = '';
  102.     originalCostElement.value = '';
  103.     sellingPriceElement.value = '';
  104.   }
  105.  
  106.   function customTd(text, parent) {
  107.     let newTd = document.createElement('td');
  108.     newTd.textContent = text;
  109.     parent.appendChild(newTd);
  110.     return newTd;
  111.   }
  112.  
  113.   function customBtn(text, parent, class1, class2) {
  114.     let newBtn = document.createElement('button');
  115.     newBtn.textContent = text;
  116.     newBtn.classList.add(class1);
  117.     newBtn.classList.add(class2);
  118.     parent.appendChild(newBtn);
  119.     return newBtn;
  120.   }
  121.  
  122.   function customSpan(text1, text2, parent) {
  123.     let newSpan = document.createElement('span');
  124.     if (text2) {
  125.       newSpan.textContent = text1 + " " + text2;
  126.     } else {
  127.       newSpan.textContent = text1;
  128.     }
  129.     parent.appendChild(newSpan);
  130.     return newSpan;
  131.   }
  132.  
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement