Advertisement
GeorgiLukanov87

01.Ski Lift - JS Advanced Exam - 18 February 2023

Mar 31st, 2023 (edited)
903
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 01.Ski Lift
  2. // JS Advanced Exam - 18 February 2023
  3. // https://judge.softuni.org/Contests/Practice/Index/3876#0
  4.  
  5. function solve() {
  6.     const [fName, lName, people, date, days] = document.querySelectorAll('input');
  7.     const nextStepBtn = document.getElementById('next-btn');
  8.     nextStepBtn.addEventListener('click', nextStepHandler);
  9.     const ticketPreview = document.querySelector('.ticket-info-list');
  10.     const confirmTicket = document.querySelector('.confirm-ticket');
  11.     const form = document.querySelector('.container-text form');
  12.     const mainElement = document.getElementById('main');
  13.     const bodyElement = document.getElementById('body');
  14.     let savedData = {};
  15.  
  16.     function nextStepHandler(e) {
  17.         e.preventDefault();
  18.         if (!fName.value || !lName.value || !people.value || !date.value || !days.value) {
  19.             alert('Fill all inputs!');
  20.             return;
  21.         }
  22.         savedData = {
  23.             fName: fName.value,
  24.             lName: lName.value,
  25.             people: people.value,
  26.             date: date.value,
  27.             days: days.value,
  28.         }
  29.  
  30.         let liContainer = customHtmlElement('li', '', '', 'ticket');
  31.         let article = customHtmlElement('article', '', liContainer);
  32.         customHtmlElement('h3', `Name: ${fName.value} ${lName.value}`, article);
  33.         customHtmlElement('p', `From date: ${date.value}`, article);
  34.         customHtmlElement('p', `For ${days.value} days`, article);
  35.         customHtmlElement('p', `For ${people.value} people`, article);
  36.         let editBtn = customHtmlElement('button', 'Edit', liContainer, 'edit-btn');
  37.         editBtn.addEventListener('click', editHandler);
  38.         let continueBtn = customHtmlElement('button', 'Continue', liContainer, 'continue-btn');
  39.         continueBtn.addEventListener('click', continueHandler);
  40.  
  41.         ticketPreview.appendChild(liContainer);
  42.         form.reset();
  43.         nextStepBtn.disabled = true;
  44.     }
  45.  
  46.     function editHandler(e) {
  47.         fName.value = savedData.fName;
  48.         lName.value = savedData.lName;
  49.         people.value = savedData.people;
  50.         date.value = savedData.date;
  51.         days.value = savedData.days;
  52.         nextStepBtn.disabled = false;
  53.         e.target.parentNode.remove();
  54.     }
  55.    
  56.     function continueHandler(e) {
  57.         confirmTicket.appendChild(e.target.parentNode);
  58.         Array.from(confirmTicket.querySelectorAll('button')).forEach(btn => { btn.remove() });
  59.  
  60.         let confirmBtn = customHtmlElement('button', 'Confirm', confirmTicket.children[0], 'confirm-btn');
  61.         confirmBtn.textContent = 'Confirm';
  62.         confirmBtn.addEventListener('click', confirmHandler);
  63.  
  64.         let cancelBtn = customHtmlElement('button', 'Cancel', confirmTicket.children[0], 'cancel-btn');
  65.         cancelBtn.addEventListener('click', cancelHandler);
  66.     }
  67.  
  68.     function confirmHandler(e) {
  69.         mainElement.remove();
  70.         customHtmlElement('h1', 'Thank you, have a nice day!', bodyElement, '', 'thank-you');
  71.         let backBtn = customHtmlElement('button', 'Back', bodyElement, '', 'back-btn');
  72.         backBtn.addEventListener('click', () => {
  73.             document.location.reload();
  74.         })
  75.     }
  76.    
  77.     function cancelHandler(e) {
  78.         e.target.parentNode.remove();
  79.         nextStepBtn.disabled = false;
  80.     }
  81.  
  82.     function customHtmlElement(type, content, parent, className, id) {
  83.         let newElement = document.createElement(type);
  84.         if (content) {
  85.             newElement.textContent = content;
  86.         }
  87.         if (className) {
  88.             newElement.classList.add(className);
  89.         }
  90.         if (parent) {
  91.             parent.appendChild(newElement);
  92.         }
  93.         if (id) {
  94.             newElement.id = id;
  95.         }
  96.         return newElement;
  97.     }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement