Advertisement
GeorgiLukanov87

01. Scary Story - JS Advanced Exam - 22 October 2022

Mar 24th, 2023
1,350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 01. Scary Story
  2. // JS Advanced Exam - 22 October 2022
  3. // https://judge.softuni.org/Contests/Practice/Index/3682#0
  4.  
  5. function solve() {
  6.   let formBtn = document.getElementById('form-btn');
  7.  
  8.   const firstName = document.getElementById('first-name');
  9.   const lastName = document.getElementById('last-name');
  10.   const age = document.getElementById('age');
  11.   const storyTitle = document.getElementById('story-title');
  12.   const genre = document.getElementById('genre');
  13.   const story = document.getElementById('story');
  14.   const previewList = document.getElementById('preview-list');
  15.   let mainElement = document.getElementById('main');
  16.   let bodyElement = document.querySelector('.body')
  17.  
  18.   let allInfor = [];
  19.  
  20.   formBtn.addEventListener('click', publishStory);
  21.  
  22.   function publishStory() {
  23.     // CHECK IF ALL FIELDS ARE VALID
  24.     let selectedGenre = genre.options[genre.selectedIndex].text;
  25.     if (firstName.value === '' || lastName.value === '' || age.value === '' ||
  26.       storyTitle.value === '' || story.value === '') {
  27.       return;
  28.     }
  29.     allInfor.push(firstName.value)
  30.     allInfor.push(lastName.value)
  31.     allInfor.push(age.value)
  32.     allInfor.push(storyTitle.value)
  33.     allInfor.push(story.value)
  34.  
  35.  
  36.     // NEW STORY HTML
  37.     let newStoryLi = document.createElement('li');
  38.     newStoryLi.classList.add('story-info');
  39.     let article = document.createElement('article');
  40.  
  41.     // HMTL ELEMENTS
  42.     let nameh4 = document.createElement('h4');
  43.     nameh4.textContent = `Name: ${firstName.value} ${lastName.value}`;
  44.  
  45.     let ageP = document.createElement('p');
  46.     ageP.textContent = `Age: ${age.value}`;
  47.  
  48.     let titleP = document.createElement('p');
  49.     titleP.textContent = `Title: ${storyTitle.value}`;
  50.  
  51.     let genreP = document.createElement('p');
  52.     genreP.textContent = `Genre : ${selectedGenre}`;
  53.  
  54.     let storyTxtP = document.createElement('p');
  55.     storyTxtP.textContent = `${story.value}`
  56.     // END HTML ELEMENTS
  57.  
  58.     // BUTTONS
  59.     let saveBtn = document.createElement('button');
  60.     saveBtn.classList.add('save-btn')
  61.     saveBtn.textContent = 'Save Story'
  62.  
  63.     let editBtn = document.createElement('button');
  64.     editBtn.classList.add('edit-btn')
  65.     editBtn.textContent = 'Edit Story'
  66.  
  67.     let deleteBtn = document.createElement('button');
  68.     deleteBtn.classList.add('delete-btn')
  69.     deleteBtn.textContent = 'Delete Story'
  70.     // END BUTTONS
  71.  
  72.     // ATTACH ALL
  73.     article.appendChild(nameh4);
  74.     article.appendChild(ageP);
  75.     article.appendChild(titleP);
  76.     article.appendChild(genreP);
  77.     article.appendChild(storyTxtP);
  78.  
  79.     newStoryLi.appendChild(article);
  80.     newStoryLi.appendChild(saveBtn);
  81.     newStoryLi.appendChild(editBtn);
  82.     newStoryLi.appendChild(deleteBtn);
  83.  
  84.     previewList.appendChild(newStoryLi);
  85.     // CLEAR INPUT FIELDS
  86.     firstName.value = '';
  87.     lastName.value = '';
  88.     age.value = '';
  89.     storyTitle.value = '';
  90.     genre.disabled = true;
  91.     story.value = '';
  92.     formBtn.disabled = true;
  93.  
  94.     // ADD EVENTS ON BUTTONS
  95.     saveBtn.addEventListener('click', saveOnClick)
  96.     editBtn.addEventListener('click', editOnClick)
  97.     deleteBtn.addEventListener('click', deleteOnClick)
  98.  
  99.     // SAVE STORY BTN
  100.     function saveOnClick() {
  101.       mainElement.remove()
  102.  
  103.       let lastBodyElement = document.createElement('div');
  104.       lastBodyElement.setAttribute('id', 'main')
  105.  
  106.       let finalH1 = document.createElement('h1');
  107.       finalH1.textContent = "Your scary story is saved!";
  108.  
  109.       lastBodyElement.appendChild(finalH1);
  110.       bodyElement.appendChild(lastBodyElement);
  111.  
  112.     }
  113.  
  114.     // EDIT STORY BTN
  115.     function editOnClick() {
  116.       firstName.value = allInfor[0]
  117.       lastName.value = allInfor[1]
  118.       age.value = allInfor[2]
  119.       storyTitle.value = allInfor[3]
  120.       story.value = allInfor[4]
  121.       formBtn.disabled = false;
  122.       genre.disabled = false;
  123.       newStoryLi.remove()
  124.     }
  125.  
  126.     // DELETE STORY BTN
  127.     function deleteOnClick() {
  128.       newStoryLi.remove()
  129.       formBtn.disabled = false;
  130.     }
  131.   }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement