John_IV

C Gift Delivery

Jun 15th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution() {
  2.     function makeEl(type, text, clazz, id) {
  3.         let el = document.createElement(type);
  4.         if (text !== null) {
  5.             el.textContent = text;
  6.         }
  7.         if (clazz !== null) {
  8.             el.setAttribute('class', clazz);
  9.         }
  10.         if (id !== null) {
  11.             el.setAttribute('id', id);
  12.         }
  13.         return el;
  14.     }
  15.  
  16.     function removeAndMoveToSection(ul, gift) {
  17.         sections[1].children[1].removeChild(gift);
  18.         gift.lastChild.remove();
  19.         gift.lastChild.remove();
  20.         ul.appendChild(gift);
  21.  
  22.         let items = [...ul.querySelectorAll('li')];
  23.         ul.innerHTML = '';
  24.         items.sort(((a, b) => a.textContent.localeCompare(b.textContent))).forEach(li => ul.appendChild(li));
  25.     }
  26.  
  27.  
  28.     let sections = document.querySelectorAll('section');
  29.     let btn = sections[0].children[1].children[1];
  30.     let input = sections[0].children[1].children[0];
  31.  
  32.  
  33.     btn.addEventListener('click', function (e) {
  34.  
  35.         if (input.value === '') {
  36.             console.log('Error');
  37.             return;
  38.         }
  39.         let giftsList = sections[1].children[1];
  40.         let gift = makeEl('li', input.value, 'gift', null);
  41.  
  42.         let sendBtn = makeEl('button', 'Send', null, 'sendButton');
  43.         sendBtn.addEventListener('click', function () {
  44.             removeAndMoveToSection(sections[2].children[1], gift);
  45.         });
  46.         gift.appendChild(sendBtn);
  47.  
  48.         let discardBtn = makeEl('button', 'Discard', null, 'discardButton');
  49.         discardBtn.addEventListener('click', function () {
  50.             removeAndMoveToSection(sections[3].children[1], gift);
  51.         });
  52.         gift.appendChild(discardBtn);
  53.  
  54.         giftsList.appendChild(gift);
  55.         let items = [...giftsList.querySelectorAll('li')];
  56.         giftsList.innerHTML = '';
  57.         items.sort(((a, b) => a.textContent.localeCompare(b.textContent))).forEach(li => giftsList.appendChild(li));
  58.         input.value = '';
  59.     });
  60. }
Add Comment
Please, Sign In to add comment