Advertisement
kstoyanov

01. Ski Inventory - 26 October 2019 - exam

Oct 22nd, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.   const addBtn = document.querySelector('#add-new button');
  3.   addBtn.addEventListener('click', addNewProduct);
  4.  
  5.   const filterBtn = document.querySelector('.filter button');
  6.   const criteria = document.querySelector('#filter');
  7.   filterBtn.addEventListener('click', filterByCriteria);
  8.  
  9.   const availableItems = document.querySelector('#products ul');
  10.   availableItems.addEventListener('click', addToBasket);
  11.  
  12.   const buyButton = document.querySelector('#myProducts button');
  13.   buyButton.addEventListener('click', buyProducts);
  14.  
  15.   const totalPrice = document.querySelectorAll('h1')[1];
  16.   let currentPrice = 0;
  17.  
  18.   function buyProducts(e) {
  19.     document.querySelector('#myProducts ul').textContent = '';
  20.     totalPrice.textContent = 'Total Price: 0.00';
  21.     currentPrice = 0;
  22.   }
  23.  
  24.   function addToBasket(e) {
  25.     if (e.target.localName === 'button') {
  26.       const ul = document.querySelector('#myProducts ul');
  27.       const li = document.createElement('li');
  28.       const strong = document.createElement('strong');
  29.  
  30.       const item = e.target.parentElement.parentElement.firstChild;
  31.       const qttyContent = e.target.parentElement.parentElement.firstChild.nextElementSibling;
  32.  
  33.       let qtty = Number(qttyContent.textContent.split(' ')[1]);
  34.  
  35.       qttyContent.textContent = `Available: ${--qtty}`;
  36.  
  37.       if (qtty < 1) {
  38.         e.target.parentElement.parentElement.remove();
  39.       }
  40.  
  41.       li.textContent = item.textContent;
  42.       strong.textContent = e.target.previousSibling.textContent;
  43.  
  44.       li.appendChild(strong);
  45.       ul.appendChild(li);
  46.  
  47.       currentPrice += Number(e.target.previousSibling.textContent);
  48.       totalPrice.textContent = `Total Price: ${currentPrice.toFixed(2)}`;
  49.     }
  50.   }
  51.  
  52.   function filterByCriteria(e) {
  53.     Array.from(availableItems.children).forEach((item) => {
  54.       if (item.firstChild.textContent.includes(criteria.value)) {
  55.         item.style.display = 'block';
  56.       } else {
  57.         item.style.display = 'none';
  58.       }
  59.     });
  60.   }
  61.  
  62.   function addNewProduct(e) {
  63.     e.preventDefault();
  64.  
  65.     const tokens = document.querySelectorAll('#add-new input');
  66.     const newItem = {
  67.       name: tokens[0].value,
  68.       qtty: Number(tokens[1].value),
  69.       price: Number(tokens[2].value),
  70.     };
  71.  
  72.     const productsList = document.querySelectorAll('#products span');
  73.  
  74.     if (newItem.name && newItem.qtty && newItem.price && ![...productsList].map((x) => x.textContent).includes(newItem.name)) {
  75.       const ul = document.querySelector('#products ul');
  76.       const li = document.createElement('li');
  77.       ul.appendChild(li);
  78.  
  79.       const span = document.createElement('span');
  80.       span.textContent = newItem.name;
  81.       li.appendChild(span);
  82.  
  83.       const strongQtty = document.createElement('strong');
  84.       strongQtty.textContent = `Available: ${newItem.qtty}`;
  85.       li.appendChild(strongQtty);
  86.  
  87.       const div = document.createElement('div');
  88.       li.appendChild(div);
  89.  
  90.       const strongPrice = document.createElement('strong');
  91.       strongPrice.textContent = `${newItem.price.toFixed(2)}`;
  92.       div.appendChild(strongPrice);
  93.  
  94.       const addToList = document.createElement('button');
  95.       addToList.textContent = "Add to Client's List";
  96.       div.appendChild(addToList);
  97.     } else if ([...productsList].map((x) => x.textContent).includes(newItem.name)) {
  98.       const index = [...productsList].map((x) => x.textContent).indexOf(newItem.name);
  99.       const element = productsList[index];
  100.  
  101.       element.nextSibling.textContent = `Available: ${newItem.qtty}`;
  102.       element.nextSibling.nextSibling.firstChild.textContent = `${newItem.price.toFixed(2)}`;
  103.     }
  104.  
  105.     tokens.forEach((t) =>{
  106.         t.value = null
  107.     });
  108.   }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement