kstoyanov

01. Central Cinema - JS Exam - 12 Aug 2020

Oct 9th, 2020 (edited)
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     const inputs = document.querySelectorAll('#container > input');
  3.     const onScreenBtn = document.querySelector('#container > button');
  4.  
  5.     const moviesOnScreensUl = document.querySelector('#movies > ul');
  6.     const archiveSection = document.querySelector('#archive > ul');
  7.  
  8.     const clearBtn = document.querySelector('#archive > button');
  9.  
  10.     onScreenBtn.addEventListener('click', addMovie);
  11.  
  12.     function addMovie(e) {
  13.         e.preventDefault();
  14.  
  15.         const movie = {
  16.             name: inputs[0].value,
  17.             hall: inputs[1].value,
  18.             ticketPrice: Number(inputs[2].value)
  19.         }
  20.  
  21.         if (!movie.name || !movie.hall || !movie.ticketPrice) {
  22.             return;
  23.         }
  24.  
  25.         const li = el('li');
  26.  
  27.         const movieNameEl = el('span', movie.name);
  28.         const hallEl = el('strong', `Hall: ${movie.hall}`);
  29.         const divEl = el('div');
  30.  
  31.         const priceEl = el('strong', movie.ticketPrice.toFixed(2));
  32.        
  33.         const inputEl = el('input');
  34.         inputEl.placeholder = "Tickets Sold";
  35.  
  36.         const archiveBtn = el('button', 'Archive');
  37.  
  38.         divEl.appendChild(priceEl);
  39.         divEl.appendChild(inputEl);
  40.         divEl.appendChild(archiveBtn);
  41.  
  42.         archiveBtn.addEventListener('click', archiveMovie);
  43.  
  44.         li.appendChild(movieNameEl);
  45.         li.appendChild(hallEl);
  46.         li.appendChild(divEl);
  47.  
  48.         moviesOnScreensUl.appendChild(li);
  49.  
  50.         inputs[0].value = '';
  51.         inputs[1].value = '';
  52.         inputs[2].value = '';
  53.  
  54.         function archiveMovie(e) {
  55.             let qtty = e.target.previousElementSibling;
  56.             if (!Number(qtty.value)) {
  57.                 return;
  58.             }
  59.  
  60.             const liEl = el('li');
  61.             const movieName = el('span', movie.name);
  62.             const strongEl = el('strong', `Total amount: ${(Number(qtty.value) * movie.ticketPrice).toFixed(2)}`);
  63.             const deleteBtn = el('button', 'Delete');
  64.  
  65.             liEl.appendChild(movieName);
  66.             liEl.appendChild(strongEl);
  67.             liEl.appendChild(deleteBtn);
  68.  
  69.             archiveSection.appendChild(liEl);
  70.             e.target.parentNode.parentNode.remove();
  71.  
  72.             deleteBtn.addEventListener('click', function (ev) {
  73.                 ev.target.parentNode.remove();
  74.             });
  75.         };
  76.     };
  77.  
  78.     clearBtn.addEventListener('click', function (e) {
  79.         e.target.previousElementSibling.textContent = '';
  80.     });
  81.  
  82.     function el(type, content, className) {
  83.         let result = document.createElement(type);
  84.  
  85.         if (content) {
  86.             result.textContent = content;
  87.         }
  88.  
  89.         if (className) {
  90.             result.className = className;
  91.         }
  92.  
  93.         return result;
  94.     }
  95. }
Add Comment
Please, Sign In to add comment