Advertisement
GeorgiLukanov87

01. SoftBlog - JS Advanced Exam - 20 February 2021

Mar 24th, 2023 (edited)
829
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 01. SoftBlog
  2. // JS Advanced Exam - 20 February 2021
  3. // https://judge.softuni.org/Contests/Practice/Index/2862#0
  4.  
  5.  
  6. function solve() {
  7.    const creatorElement = document.getElementById('creator');
  8.    const titleElement = document.getElementById('title');
  9.    const categoryElement = document.getElementById('category');
  10.    const contentElement = document.getElementById('content');
  11.  
  12.    const blogPostContainer = document.querySelector('body > div > div > main > section');
  13.    const archiveSection = document.querySelector('.archive-section>ol');
  14.    document.querySelector('button.create').addEventListener('click', createPost);
  15.    let allTitles = [];
  16.  
  17.    let data = {};
  18.    function createPost(e) {
  19.       e.preventDefault();
  20.       data = {
  21.          creator: creatorElement.value,
  22.          title: titleElement.value,
  23.          category: categoryElement.value,
  24.          content: contentElement.value,
  25.       }
  26.  
  27.       let newArticle = document.createElement('article');
  28.       newArticle.innerHTML = `
  29.       <h1>${data.title}</h1>
  30.       <p>Category:
  31.          <strong>${data.category}</strong>
  32.       </p>
  33.       <p>Creator:
  34.       <strong>${data.creator}</strong>
  35.       </p>
  36.       <p>${data.content}</p>`
  37.  
  38.       let btnContainerDiv = document.createElement('div');
  39.       btnContainerDiv.classList.add('buttons');
  40.  
  41.       let delBtn = document.createElement('button');
  42.       delBtn.textContent = 'Delete';
  43.       delBtn.classList.add('btn');
  44.       delBtn.classList.add('delete');
  45.       delBtn.addEventListener('click', deletePost);
  46.  
  47.       let archiveBtn = document.createElement('button');
  48.       archiveBtn.textContent = 'Archive';
  49.       archiveBtn.classList.add('btn');
  50.       archiveBtn.classList.add('archive');
  51.       archiveBtn.addEventListener('click', archivePost);
  52.  
  53.       btnContainerDiv.appendChild(delBtn);
  54.       btnContainerDiv.appendChild(archiveBtn);
  55.       newArticle.appendChild(btnContainerDiv);
  56.  
  57.       blogPostContainer.appendChild(newArticle);
  58.       clearInputs();
  59.  
  60.    }
  61.    function deletePost(e) {
  62.       e.target.parentNode.parentNode.remove();
  63.    }
  64.  
  65.    function archivePost(e) {
  66.       let articleToMove = e.target.parentNode.parentNode.children[0].textContent;
  67.       allTitles.push(articleToMove);
  68.       let sortedTitles = allTitles.sort((a, b) => a.localeCompare(b));
  69.  
  70.       archiveSection.innerHTML = '';
  71.       for (let i = 0; i < sortedTitles.length; i++) {
  72.          let newLi = document.createElement('li');
  73.          newLi.textContent = sortedTitles[i];
  74.          archiveSection.appendChild(newLi);
  75.       }
  76.  
  77.       e.target.parentNode.parentNode.remove();
  78.    }
  79.  
  80.    function clearInputs() {
  81.       creatorElement.value = '';
  82.       titleElement.value = '';
  83.       categoryElement.value = '';
  84.       contentElement.value = '';
  85.    }
  86.  
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement