Advertisement
VeselaVideva

Advanced Functions - Exercise - 09. Central Cinema

Feb 4th, 2021
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     const [nameInput, hallInput, priceInput] = document.querySelectorAll('#container input');
  3.     const moviesUl = document.querySelector('#movies ul');
  4.     const archiveUl = document.querySelector('#archive ul');
  5.     moviesUl.innerHTML = '';
  6.     archiveUl.innerHTML = '';
  7.     const clearBtn = document.querySelector('#archive button');
  8.     clearBtn.addEventListener('click', clearMovies);
  9.     const addBtn = document.querySelector('#container button');
  10.     addBtn.addEventListener('click', addMovie);
  11.  
  12.     function clearMovies() {
  13.         archiveUl.innerHTML = '';
  14.     }
  15.  
  16.     function addMovie(e) {
  17.         e.preventDefault();
  18.         if (nameInput.value.trim() === '' || hallInput.value.trim() === '' || !Number(priceInput.value)) {
  19.             return;
  20.         }
  21.  
  22.         const li = document.createElement('li');
  23.         const span = document.createElement('span');
  24.         span.textContent = `${nameInput.value}`;
  25.         const strong = document.createElement('strong');
  26.         strong.textContent = `Hall: ${hallInput.value}`;
  27.         const div = document.createElement('div');
  28.         const archiveBtn = document.createElement('button');
  29.         archiveBtn.textContent = 'Archive';
  30.         archiveBtn.addEventListener('click', archiveMovie);
  31.         const strongDiv = document.createElement('strong');
  32.         strongDiv.textContent = `${Number(priceInput.value).toFixed(2)}`;
  33.         const input = document.createElement('input');
  34.         input.placeholder = 'Tickets Sold';
  35.  
  36.         div.appendChild(strongDiv);
  37.         div.appendChild(input);
  38.         div.appendChild(archiveBtn);
  39.         li.appendChild(span);
  40.         li.appendChild(strong);
  41.         li.appendChild(div);
  42.         moviesUl.appendChild(li);
  43.  
  44.         nameInput.value = '';
  45.         hallInput.value = '';
  46.         priceInput.value = '';
  47.  
  48.         function archiveMovie(e) {
  49.  
  50.             if (!Number(input.value)) {
  51.                 return;
  52.             }
  53.  
  54.             strong.textContent = `Total amount: ${(Number(strongDiv.textContent) * Number(input.value)).toFixed(2)}`;
  55.             const deleteBtn = document.createElement('button');
  56.             deleteBtn.textContent = 'Delete';
  57.             deleteBtn.addEventListener('click', deleteMovie);
  58.             li.appendChild(deleteBtn);
  59.             archiveUl.appendChild(li);
  60.  
  61.             div.remove();
  62.         }
  63.  
  64.         function deleteMovie() {
  65.             li.remove();
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement