Advertisement
Guest User

Untitled

a guest
May 6th, 2020
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     let oldBooks = document.querySelectorAll('#outputs .bookShelf')[0];
  3.     let newBooks = document.querySelectorAll('#outputs .bookShelf')[1];
  4.     document.querySelector('body form button').addEventListener('click', addBook);
  5.     function addBook(e) {
  6.         e.preventDefault();
  7.         const fields = document.querySelectorAll('body form input');
  8.         const title = fields[0].value;
  9.         const year = Number(fields[1].value);
  10.         let price = Number(fields[2].value);
  11.         //triple condition
  12.         if(title === '' || year < 1 || price <= 0) { return; }
  13.  
  14.         const book = createBook(title,year,price);
  15.         year >= 2000 ? newBooks.appendChild(book) : oldBooks.appendChild(book);
  16.     }
  17.  
  18.     function createBook(title, year, price) {
  19.         let divBook = document.createElement('div');
  20.         divBook.className = 'book';
  21.  
  22.         let paragraphBook = document.createElement('p');
  23.         paragraphBook.textContent = `${title} [${year}]`;
  24.         divBook.appendChild(paragraphBook);
  25.  
  26.         year < 2000 ? price = discount(price) : price;
  27.         let buttonBuyBook = addButton(`Buy it only for ${price.toFixed(2)} BGN`);
  28.         buttonBuyBook.addEventListener('click', buyBook);
  29.         divBook.appendChild(buttonBuyBook);
  30.        
  31.         if(year >= 2000) {
  32.             let buttonOldSection = addButton('Move to old section');
  33.             buttonOldSection.addEventListener('click', toOldSection);
  34.             divBook.appendChild(buttonOldSection);
  35.         }
  36.        
  37.         return divBook;
  38.     }
  39.  
  40.     function discount(price) {
  41.         return price * 0.85;
  42.     }
  43.  
  44.     function addButton(context) {
  45.         let button = document.createElement('button');
  46.         button.textContent = context;
  47.         return button;
  48.     }
  49.  
  50.     function buyBook(e) {
  51.         let storeProfit = document.querySelectorAll('body h1')[1];
  52.         let oldProfit = Number(storeProfit.textContent.split(' ')[3]);
  53.  
  54.         let bookPrice = Number(this.textContent.split(' ')[4]);
  55.         storeProfit.textContent = `Total Store Profit: ${(oldProfit + bookPrice).toFixed(2)} BGN`;
  56.         this.parentNode.remove();
  57.     }
  58.  
  59.     function toOldSection(e) {
  60.         let bookMoved = this.parentNode;
  61.         //remove only button to old sectio!!!
  62.         this.remove();
  63.  
  64.         let buyBook = bookMoved.querySelector('button');
  65.         let oldPrice = Number(buyBook.textContent.split(' ')[4]);
  66.         buyBook.textContent = `Buy it only for ${discount(oldPrice).toFixed(2)} BGN`;
  67.         oldBooks.appendChild(bookMoved);
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement