SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     const addBtn = document.querySelector("body > form > button");
  3.     let [bookInput, yearInput, priceInput] = Array.from(document.querySelectorAll("form > input"));
  4.     const bookShelfOldBooks = document.querySelector("#outputs > section:nth-child(1) > div");
  5.     const bookShelfNewBooks = document.querySelector("#outputs > section:nth-child(2) > div");
  6.     let priceWrapper = document.querySelector("body > h1:nth-child(3)");
  7.     let price = 0;
  8.  
  9.     addBtn.addEventListener('click', (e) => {
  10.         e.preventDefault();
  11.         checkForValidInputs();
  12.     });
  13.  
  14.     function checkForValidInputs() {
  15.         let year = Number(yearInput.value);
  16.         let price = Number(priceInput.value);
  17.         let type = '';
  18.         if (bookInput.value !== '' && year > 0 && price > 0) {
  19.             if (year >= 2000) {
  20.                 type = 'new';
  21.             } else {
  22.                 type = 'old';
  23.             }
  24.             addNewBook(type, year, price);
  25.         }
  26.     }
  27.  
  28.     function addNewBook(type, y, p) {
  29.         let divBook = document.createElement('div');
  30.         divBook.classList.add('book');
  31.  
  32.         let par = document.createElement('p');
  33.         par.textContent = `${bookInput.value} [${y}]`;
  34.  
  35.         let buyBtn = document.createElement('button');
  36.         buyBtn.textContent = `Buy it only for ${p.toFixed(2)} BGN`;
  37.         buyBtn.addEventListener('click',  removeBookAndIcreaseProfit);
  38.  
  39.  
  40.         divBook.appendChild(par);
  41.         divBook.appendChild(buyBtn);
  42.  
  43.         if (type === 'new') {
  44.             let newBooksBtn = document.createElement('button');
  45.             newBooksBtn.textContent = `Move to old section`;
  46.             newBooksBtn.addEventListener('click', () => moveToOldSection(divBook, newBooksBtn, p));
  47.  
  48.             divBook.appendChild(newBooksBtn);
  49.  
  50.             bookShelfNewBooks.appendChild(divBook);
  51.         } else {
  52.             p = p * 0.85;
  53.             buyBtn.textContent = `Buy it only for ${p.toFixed(2)} BGN`;
  54.             bookShelfOldBooks.appendChild(divBook);
  55.  
  56.         }
  57.  
  58.     }
  59.  
  60.     function removeBookAndIcreaseProfit(e) {
  61.         //Matching the current price
  62.         let pr = Number(e.target.innerHTML.match(/\d+\.\d+/g).join(''));
  63.         price += pr;
  64.         priceWrapper.textContent = `Total Store Profit: ${+price.toFixed(2)} BGN`;
  65.         //Removing a div from ANY bookshelf
  66.         let parent = e.target.parentNode.parentNode;
  67.         let index = findElIndex(Array.from(parent.children), e.target.parentNode);
  68.         removeEl(parent, index);
  69.  
  70.     }
  71.     //Finding the index of the correct div child
  72.     function findElIndex(parent, child) {
  73.         return parent.findIndex((e) => e === child);
  74.     }
  75.     //Removing a child from the parent div at the given index
  76.     function removeEl(parent, index) {
  77.         parent.removeChild(parent.children[index]);
  78.     }
  79.  
  80.     function moveToOldSection(child, btnToRemove, p) {
  81.         //Decreasing the price when moved to old section
  82.         p = p * 0.85;
  83.         bookShelfNewBooks.removeChild(child);
  84.         child.removeChild(btnToRemove);
  85.         bookShelfOldBooks.appendChild(child);
  86.         child.lastChild.innerHTML = `Buy it only for ${p.toFixed(2)} BGN`;
  87.     }
  88. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top