John_IV

Untitled

Jun 13th, 2020
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     function makeEl(type, text, clazz) {
  3.         let el = document.createElement(type);
  4.         if (text !== null) {
  5.             el.textContent = text;
  6.         }
  7.         if (clazz !== null) {
  8.             el.setAttribute('class', clazz);
  9.         }
  10.         return el;
  11.     }
  12.     function makeBuyBtn(price, bookDiv) {
  13.         let buyBtn = makeEl('button', `Buy it only for ${price} BGN`, null);
  14.         buyBtn.addEventListener('click', function () {
  15.             bookDiv.remove();
  16.             profit += +price;
  17.             document.getElementsByTagName('h1')[1].textContent = `Total Store Profit: ${profit} BGN`;
  18.         });
  19.         return buyBtn;
  20.     }
  21.  
  22.     let btn = document.getElementsByTagName('button')[0];
  23.     let oldBookShelf = document.getElementsByClassName('bookShelf')[0];
  24.     let newBookShelf = document.getElementsByClassName('bookShelf')[1];
  25.     let profit = 0;
  26.  
  27.     btn.addEventListener('click', function (e) {
  28.         e.preventDefault();
  29.  
  30.         let [name, year, price] = Array.from(document.getElementsByTagName('input')).map(e => e.value);
  31.  
  32.         if (name === '' || year <= 0 || price <= 0) {
  33.             console.log('Error');
  34.             return;
  35.         }
  36.         let bookDiv = makeEl('div', null, 'book');
  37.         bookDiv.appendChild(makeEl('p', `${name} [${year}]`, null));
  38.  
  39.         let priceNew = parseFloat(price).toFixed(2);
  40.         let priceOld = (priceNew * 0.85).toFixed(2);
  41.  
  42.         if (+year < 2000) {
  43.             bookDiv.appendChild(makeBuyBtn(priceOld, bookDiv));
  44.             oldBookShelf.appendChild(bookDiv);
  45.  
  46.         } else {
  47.             bookDiv.appendChild(makeBuyBtn(priceNew, bookDiv));
  48.  
  49.             let moveBtn = makeEl('button', `Move to old section`, null);
  50.             moveBtn.addEventListener('click', function () {
  51.                 bookDiv.remove();
  52.                 bookDiv.lastChild.remove();
  53.                 bookDiv.lastChild.remove();
  54.                 bookDiv.appendChild(makeBuyBtn(priceOld, bookDiv));
  55.                 oldBookShelf.appendChild(bookDiv);
  56.             });
  57.             bookDiv.appendChild(moveBtn);
  58.             newBookShelf.appendChild(bookDiv);
  59.         }
  60.     });
  61. }
Advertisement
Add Comment
Please, Sign In to add comment