Advertisement
GeorgiLukanov87

01. Christmas Gifts Delivery - JS Advanced - Exam Prep - 15.02.2021

Mar 24th, 2023 (edited)
812
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 01. Christmas Gifts Delivery
  2. // JS Advanced - Exam Prep - 15.02.2021
  3. // https://judge.softuni.org/Contests/Practice/Index/2861#0
  4.  
  5. function solution() {
  6.     let sections = document.querySelectorAll('section');
  7.     let addBtn = sections[0].children[1].children[1];
  8.     let inputField = sections[0].children[1].children[0];
  9.  
  10.     addBtn.addEventListener('click', addGift);
  11.     let allGifts = [];
  12.  
  13.     function addGift() {
  14.         let giftName = inputField.value;
  15.         allGifts.push(giftName);
  16.         let listOfGiftSec = sections[1].querySelector('ul');
  17.         let sortedGifts = allGifts.sort((a, b) => a.localeCompare(b));
  18.         listOfGiftSec.innerHTML = '';
  19.  
  20.         for (let i = 0; i < sortedGifts.length; i++) {
  21.             let newLi = document.createElement('li');
  22.             newLi.classList.add('gift');
  23.             newLi.textContent = sortedGifts[i];
  24.  
  25.             let sendBtn = createCustomBtn('sendButton', 'Send', newLi);
  26.             sendBtn.addEventListener('click', sendGift)
  27.             let discardBtn = createCustomBtn('discardButton', 'Discard', newLi);
  28.             discardBtn.addEventListener('click', discartGift);
  29.  
  30.             newLi.appendChild(sendBtn);
  31.             newLi.appendChild(discardBtn);
  32.             listOfGiftSec.appendChild(newLi);
  33.         }
  34.  
  35.         inputField.value = '';
  36.     }
  37.  
  38.     function sendGift(e) {
  39.         let currentItem = e.target.parentNode
  40.         let sendGifts = sections[2].querySelector('ul');
  41.         Array.from(currentItem.querySelectorAll('button')).forEach(bnt => { bnt.remove() });
  42.         updateGiftList(currentItem.textContent)
  43.         sendGifts.appendChild(currentItem);
  44.     }
  45.  
  46.     function discartGift(e) {
  47.         let currentItem = e.target.parentNode
  48.         let discartedGifts = sections[3].querySelector('ul');
  49.         Array.from(currentItem.querySelectorAll('button')).forEach(bnt => { bnt.remove() });
  50.         updateGiftList(currentItem.textContent)
  51.         discartedGifts.appendChild(currentItem);
  52.     }
  53.  
  54.     function updateGiftList(item) {
  55.         let index = allGifts.indexOf(item);
  56.         allGifts.splice(index, 1);
  57.     }
  58.  
  59.     function createCustomBtn(id, content, parent) {
  60.         let newBtn = document.createElement('button');
  61.         newBtn.id = id;
  62.         newBtn.textContent = content;
  63.         parent.appendChild(newBtn);
  64.         return newBtn;
  65.     }
  66. }
Advertisement
Comments
  • AlexHudson124
    1 year
    # text 0.19 KB | 0 0
    1. 101.166.94.231:27030 (Port can change)
    2.  
    3. Please join in gmod i self-host servers. If i like you you can have one if not you have to make me an offer and i host it anyways, my provider is stupid!
Add Comment
Please, Sign In to add comment
Advertisement