valiamaximova1

01. Christmas Gifts Delivery

Feb 19th, 2021 (edited)
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution() {
  2.     const [gifts, sent, discarded] = document.querySelectorAll('section ul');
  3.     const input = document.querySelector('input');
  4.     document.querySelector('button').addEventListener('click', addEl);
  5.  
  6.     function addEl() {
  7.         const name = input.value;
  8.         input.value = '';
  9.  
  10.         const element = e('li', name, 'gift')
  11.         const sentBtn = e('button', 'Send', 'sendButton');
  12.         const discardedBtn = e('button', 'Discard', 'discardButton');
  13.  
  14.         element.appendChild(sentBtn);
  15.         element.appendChild(discardedBtn);
  16.         gifts.appendChild(element);
  17.  
  18.         sorted();
  19.         sentBtn.addEventListener('click', () => sentLogic(name, element));
  20.         discardedBtn.addEventListener('click', () => discardedButton(name, element));
  21.     }
  22.  
  23.     function sentLogic(name, gift) {
  24.         gift.remove();
  25.         const el = e('li', name, 'gift');
  26.         sent.appendChild(el);
  27.  
  28.     }
  29.  
  30.     function discardedButton(name, gift) {
  31.         gift.remove();
  32.         const el = e('li', name, 'gift');
  33.         discarded.appendChild(el);
  34.     }
  35.  
  36.     function sorted() {
  37.         Array
  38.             .from(gifts.children)
  39.             .sort((a, b) => a.textContent.localeCompare(b.textContent))
  40.             .forEach(e => gifts.appendChild(e));
  41.  
  42.     }
  43.  
  44.     function e(type, content, className) {
  45.         const result = document.createElement(type);
  46.         result.textContent = content;
  47.         if (className) {
  48.             result.className = className;
  49.         }
  50.         return result;
  51.     }
  52. }
Add Comment
Please, Sign In to add comment