simeonshopov

Central Cinema

Jun 10th, 2021
560
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     document.querySelector('#container button').addEventListener('click', createMovie);
  3.     document.querySelector('#add-new').addEventListener('submit', e => e.preventDefault());
  4.     document.querySelector('#archive > button').addEventListener('click', clearArchive);
  5.  
  6.     function createMovie() {
  7.         const fields = Array.from(document.querySelectorAll('#container input'));
  8.         const values = fields.map(x => x.value);
  9.         let [movieName, hall, ticketPrice] = values;
  10.         ticketPrice = Number(ticketPrice).toFixed(2);
  11.         const gotEverything = values.filter(x => x != '').length == 3 && !isNaN(values[2]);
  12.         if (gotEverything) { createLi(); fields.forEach(field => field.value = ''); }
  13.  
  14.         function createLi() {
  15.             const mainEl = document.createElement('li');
  16.             const outerSpanEl = document.createElement('span');
  17.             const outerStrongEl = document.createElement('strong');
  18.             const divEl = document.createElement('div');
  19.             const divStrongEl = document.createElement('strong');
  20.             const divButtonEl = document.createElement('button');
  21.             const divInputEl = document.createElement('input');
  22.             outerSpanEl.textContent = movieName;
  23.             outerStrongEl.textContent = `Hall: ${hall}`;
  24.             divStrongEl.textContent = ticketPrice;
  25.             divInputEl.placeholder = 'Tickets Sold';
  26.             divButtonEl.textContent = 'Archive';
  27.             divButtonEl.addEventListener('click', archiveMovie);
  28.             divEl.appendChild(divStrongEl);
  29.             divEl.appendChild(divInputEl);
  30.             divEl.appendChild(divButtonEl);
  31.             mainEl.appendChild(outerSpanEl);
  32.             mainEl.appendChild(outerStrongEl);
  33.             mainEl.appendChild(divEl);
  34.             document.querySelector('#movies ul').appendChild(mainEl);
  35.         }
  36.     }
  37.  
  38.     function archiveMovie() {
  39.         const tickets = this.parentNode.parentNode.querySelector('input').value;
  40.         if (!isNaN(tickets) && tickets != '') {
  41.             const mainParentEl = this.parentNode.parentNode;
  42.             const price = Number(this.parentNode.querySelector('strong').textContent);
  43.             const name = mainParentEl.querySelector('span').textContent;
  44.             const totalProfit = (price * tickets).toFixed(2);
  45.             document.querySelector('#archive > ul').appendChild(createEl(name, totalProfit));
  46.             mainParentEl.remove();
  47.         }
  48.  
  49.         function createEl(name, totalProfit) {
  50.             const liEL = document.createElement('li');
  51.             const spanEl = document.createElement('span');
  52.             const strongEl = document.createElement('strong');
  53.             const btnEl = document.createElement('button');
  54.             spanEl.textContent = name;
  55.             strongEl.textContent = `Total amount: ${totalProfit}`;
  56.             btnEl.textContent = 'Delete';
  57.             btnEl.addEventListener('click', deleteMovie);
  58.             liEL.appendChild(spanEl);
  59.             liEL.appendChild(strongEl);
  60.             liEL.appendChild(btnEl);
  61.             return liEL;
  62.         }
  63.     }
  64.  
  65.     function deleteMovie() {
  66.         this.parentNode.remove();
  67.     }
  68.  
  69.     function clearArchive() {
  70.         const movies = document.querySelector('#archive > ul');
  71.         while (movies.firstChild) {movies.removeChild(movies.lastChild);}
  72.     }
  73. }
RAW Paste Data