Advertisement
didkoslawow

Untitled

Jun 10th, 2023
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     const furnitureList = document.getElementById('furniture-list');
  3.     const totalPriceElement = document.querySelector('.total-price');
  4.  
  5.     const modelInput = document.getElementById('model');
  6.     const yearInput = document.getElementById('year');
  7.     const descriptionInput = document.getElementById('description');
  8.     const priceInput = document.getElementById('price');
  9.  
  10.     const addBtn = document.getElementById('add');
  11.  
  12.     addBtn.addEventListener('click', onAdd);
  13.  
  14.     function onAdd(e) {
  15.         e.preventDefault();
  16.  
  17.         const model = modelInput.value.trim();
  18.         const year = yearInput.value.trim();
  19.         const description = descriptionInput.value.trim();
  20.         const price = priceInput.value.trim();
  21.  
  22.         if (model == '' || year == '' || description == '' || price == '') {
  23.             return;
  24.         }
  25.  
  26.         if (Number(year) < 0 || Number(price) < 0) {
  27.             return;
  28.         }
  29.  
  30.         const infoTr = createElement('tr', null, { class: 'info' }, [
  31.             createElement('td', model),
  32.             createElement('td', Number(price).toFixed(2)),
  33.             createElement('td', null, null, [
  34.                 createElement('button', 'More Info', { class: 'moreBtn', onclick: onMoreInfo }),
  35.                 createElement('button', 'Buy it', { class: 'buyBtn', onclick: onBuyProduct }),
  36.             ]),
  37.         ]);
  38.         const hideTr = createElement('tr', null, { class: 'hide' }, [
  39.             createElement('td', 'Year: ' + year),
  40.             createElement('td', 'Description: ' + description, { colspan: '3' }),
  41.         ]);
  42.  
  43.         furnitureList.appendChild(infoTr);
  44.         furnitureList.appendChild(hideTr);
  45.  
  46.         clearInputFields();
  47.  
  48.         function onMoreInfo(e) {
  49.             if (e.target.textContent == 'More Info') {
  50.                 e.target.textContent = 'Less Info';
  51.                 hideTr.style.display = 'contents';
  52.             } else {
  53.                 e.target.textContent = 'More Info';
  54.                 hideTr.style.display = 'none';
  55.             }
  56.         }
  57.  
  58.         function onBuyProduct() {
  59.             totalPriceElement.textContent = (Number(totalPriceElement.textContent) + Number(price)).toFixed(2);
  60.  
  61.             infoTr.remove();
  62.             hideTr.remove();
  63.         }
  64.     }
  65.  
  66.     function createElement(tagName, textContent, attributes, children = []) {
  67.         const element = document.createElement(tagName);
  68.         const PARAMS = {
  69.             colspan: (value) => element.setAttribute('colspan', value),
  70.             class: (value) => element.classList.add(value),
  71.             id: (value) => (element.id = value),
  72.             onclick: (value) => element.addEventListener('click', value),
  73.             disabled: () => element.setAttribute('disabled', ''),
  74.             src: (value) => element.setAttribute('src', value),
  75.         };
  76.  
  77.         if (textContent) {
  78.             element.textContent = textContent;
  79.         }
  80.  
  81.         if (attributes) {
  82.             Object.entries(attributes).forEach(([param, value]) => PARAMS[param](value));
  83.         }
  84.  
  85.         if (children.length == 0) {
  86.             return element;
  87.         }
  88.  
  89.         children.forEach((c) => element.appendChild(c));
  90.  
  91.         return element;
  92.     }
  93.  
  94.     function clearInputFields() {
  95.         modelInput.value = '';
  96.         yearInput.value = '';
  97.         descriptionInput.value = '';
  98.         priceInput.value = '';
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement