Advertisement
GeorgiLukanov87

01. Furniture Store - JS Advanced Final Retake Exam - 12 August 2021

Mar 23rd, 2023 (edited)
830
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 01. Furniture Store
  2. // JS Advanced Final Retake Exam - 12 August 2021
  3. // https://judge.softuni.org/Contests/Practice/Index/3089#0
  4.  
  5. function solve() {
  6.     const modelElement = document.getElementById('model');
  7.     const yearElement = document.getElementById('year');
  8.     const descriptionElement = document.getElementById('description');
  9.     const priceElement = document.getElementById('price');
  10.  
  11.     const furniteListElement = document.getElementById('furniture-list');
  12.     const totalPriceElement = document.getElementsByClassName('total-price')[0];
  13.     document.getElementById('add').addEventListener('click', addItem);
  14.     let prices = [];
  15.  
  16.     function addItem(e) {
  17.         e.preventDefault();
  18.         let data = {
  19.             model: modelElement.value,
  20.             year: Number(yearElement.value),
  21.             description: descriptionElement.value,
  22.             price: Number(priceElement.value),
  23.         }
  24.         if (data.model == '' || data.year == '' || data.description == '' || data.price == '' ||
  25.             data.price < 0 || data.year < 0) {
  26.             alert('Missing data or year/price must be positive number');
  27.         }
  28.        
  29.         let trInfo = customHTMLelement('tr', '', 'info');
  30.         customHTMLelement('td', data.model, '', trInfo);
  31.         customHTMLelement('td', data.price.toFixed(2), '', trInfo);
  32.  
  33.         let btnsContainer = document.createElement('td');
  34.         let moreBtn = customHTMLelement('button', 'More Info', 'moreBtn');
  35.         moreBtn.addEventListener('click', moreInfo)
  36.         btnsContainer.appendChild(moreBtn)
  37.         let buyBtn = customHTMLelement('button', 'Buy it', 'buyBtn');
  38.         buyBtn.addEventListener('click', buyIt)
  39.         btnsContainer.appendChild(buyBtn)
  40.  
  41.         trInfo.appendChild(btnsContainer);
  42.         furniteListElement.appendChild(trInfo);
  43.  
  44.         let hideTr = customHTMLelement('tr', '', 'hide');
  45.         let hideYearTd = customHTMLelement('td', data.year, '', hideTr, 'Year');
  46.         let hideDscrTd = customHTMLelement('td', data.description, '', '', 'Description');
  47.         hideDscrTd.setAttribute('colspan', 3);
  48.         hideTr.appendChild(hideDscrTd);
  49.  
  50.         furniteListElement.appendChild(hideTr)
  51.     }
  52.  
  53.     function moreInfo(e) {
  54.         let moreInfoBtn = e.target;
  55.         let hideTrSibling = moreInfoBtn.parentNode.parentNode.nextElementSibling;
  56.         if (moreInfoBtn.textContent == 'More Info') {
  57.             moreInfoBtn.textContent = 'Less Info';
  58.             hideTrSibling.style.display = 'contents';
  59.         } else {
  60.             moreInfoBtn.textContent = 'More Info';
  61.             hideTrSibling.style.display = 'none';
  62.         }
  63.     }
  64.  
  65.     function buyIt(e) {
  66.         let price = e.target.parentNode.parentNode.children[1].textContent
  67.         prices.push(Number(price));
  68.         let totalSum = prices.reduce((a, b) => a + b, 0);
  69.         totalPriceElement.textContent = totalSum.toFixed(2);
  70.         e.target.parentNode.parentNode.remove();
  71.     }
  72.  
  73.     function customHTMLelement(type, content, className, parent, extraInfo) {
  74.         let newElement = document.createElement(type);
  75.         if (extraInfo) {
  76.             newElement.textContent = `${extraInfo}: ${content}`;
  77.         } else {
  78.             newElement.textContent = content
  79.         }
  80.         if (className) {
  81.             newElement.classList.add(className);
  82.         }
  83.         if (parent) {
  84.             parent.appendChild(newElement);
  85.         }
  86.         return newElement;
  87.     }
  88.  
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement