Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     const inputs = document.querySelectorAll('form > input');
  3.     const book = inputs[0];
  4.     const year = inputs[1];
  5.     const price = inputs[2];
  6.     const output = document.querySelector('#outputs').children;
  7.     const oldBooksSection = output[0].querySelector('.bookShelf');
  8.     const newBooksSection = output[1].querySelector('.bookShelf');
  9.     const profit = document.querySelectorAll('h1')[1];
  10.  
  11.     function createBook(b, y, pr) {
  12.         pr = (y >= 2000
  13.             ? Number(pr)
  14.             : Number(pr) * 0.85).toFixed(2);
  15.  
  16.         const div = document.createElement('div');
  17.         div.classList.add('book');
  18.         const p = document.createElement('p');
  19.         p.innerHTML = `${b} [${y}]`;
  20.         const btnBuy = document.createElement('button');
  21.         btnBuy.innerHTML = `Buy it only for ${pr} BGN`;
  22.        
  23.         div.appendChild(p);
  24.         div.appendChild(btnBuy);
  25.  
  26.         btnBuy.addEventListener('click', (ev) => {
  27.             const arr = profit.innerHTML.split(' ');
  28.             arr[3] = Number(arr[3]) + Number(pr);
  29.             profit.innerHTML = arr.join(' ');
  30.             ev.target.parentNode.remove();
  31.         });
  32.  
  33.         if(y >= 2000){
  34.             const btnMove = document.createElement('button');
  35.             btnMove.innerHTML = 'Move to old section';
  36.             div.appendChild(btnMove);
  37.             btnMove.addEventListener('click', (ev) => {
  38.                 pr = (Number(pr) * 0.85).toFixed(2);
  39.                 const arr = ev.target.parentNode.children[1].innerHTML.split(' ');
  40.                 arr[4] =(Number(arr[4]) * 0.85).toFixed(2);
  41.                 ev.target.parentNode.children[1].innerHTML = arr.join(' ');
  42.                 oldBooksSection.appendChild(ev.target.parentNode);
  43.                 ev.target.remove();
  44.             });
  45.         }
  46.         return div;
  47.     }
  48.  
  49.     function handler(ev) {
  50.         ev.preventDefault();
  51.         if (book.value !== '' &&
  52.             year.value > 0 &&
  53.             price.value > 0) {
  54.             if (year.value >= 2000) {
  55.                 newBooksSection.appendChild(createBook(book.value, year.value, price.value));
  56.             }else {
  57.                 oldBooksSection.appendChild(createBook(book.value, year.value, price.value));
  58.             }
  59.         }
  60.     }
  61.     document.querySelector('form > button').addEventListener('click', handler);
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement