SHARE
TWEET

Untitled

a guest Oct 22nd, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.   const oldBooks = document.getElementById('outputs').firstElementChild.lastElementChild;
  3.   const newBooks = document.getElementById('outputs').lastElementChild.lastElementChild;
  4.   const setMulti = (elem, atr) => atr.map(e => elem.setAttribute(e[0], e[1]));
  5.   let total = 0;
  6.   const createElement = (elem, text) => {
  7.     let e = document.createElement(elem);
  8.     e.textContent = text;
  9.     return e;
  10.   };
  11.  
  12.   function validate(e) {
  13.     e.preventDefault();
  14.     const name = document.getElementsByTagName('input')[0].value;
  15.     const year = document.getElementsByTagName('input')[1].value;
  16.     const price = document.getElementsByTagName('input')[2].value;
  17.  
  18.     if (typeof name === 'string' && name.length > 0 && !isNaN(year) && !isNaN(price)) {
  19.       createBook(name, year, price);
  20.     }
  21.   }
  22.  
  23.   function createNewBook(div, price) {
  24.     div.appendChild(createElement('button', `Buy it only for ${Number(price).toFixed(2)} BGN`));
  25.     div.appendChild(createElement('button', 'Move to old section'));
  26.     newBooks.appendChild(div);
  27.     Array.from(newBooks.getElementsByTagName('button')).map(e => e.addEventListener('click', handleBooks));
  28.   }
  29.  
  30.   function createOldBook(div, price) {
  31.     div.appendChild(createElement('button', `Buy it only for ${Number(price * 0.85).toFixed(2)} BGN`));
  32.     oldBooks.appendChild(div);
  33.     Array.from(oldBooks.getElementsByTagName('button')).map(e => e.addEventListener('click', handleBooks));
  34.   }
  35.  
  36.   function moveBook(elem, price) {
  37.     let div = elem.parentElement;
  38.     div.removeChild(elem);
  39.     div.lastElementChild.textContent = `Buy it only for ${Number(price * 0.85).toFixed(2)} BGN`;
  40.     oldBooks.appendChild(div);
  41.   }
  42.  
  43.   function createBook(name, year, price) {
  44.     const div = document.createElement('div');
  45.     setMulti(div, [['class', 'book']]);
  46.     div.appendChild(createElement('p', `${name} [${year}]`));
  47.     if (year > 1999) {
  48.       createNewBook(div, price);
  49.     } else {
  50.       createOldBook(div, price);
  51.     }
  52.   }
  53.  
  54.   function handleBooks(e) {
  55.     if (e.target.textContent.includes('Buy')) {
  56.       let price = e.target.textContent.match(/[0-9]+\.[0-9]+/)[0];
  57.       total += Number(price);
  58.       document.getElementsByTagName('h1')[1].textContent = `Total Store Profit: ${total.toFixed(2)} BGN`;
  59.       e.target.parentElement.remove();
  60.     } else {
  61.       let price = e.target.previousElementSibling.textContent.match(/[0-9]+\.[0-9]+/)[0];
  62.       moveBook(e.target, price);
  63.     }
  64.   }
  65.   document.getElementsByTagName('button')[0].addEventListener('click', validate);
  66. }
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