Advertisement
GeorgiLukanov87

01.Hotel Reservation - JS Advanced Retake Exam - 7 December 2022

Mar 20th, 2023 (edited)
776
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 01.Hotel Reservation
  2. // JS Advanced Retake Exam - 7 December 2022
  3. // https://judge.softuni.org/Contests/Practice/Index/3732#0
  4.  
  5. function solve() {
  6.     nextBtn = document.querySelector('#next-btn');
  7.  
  8.     firstName = document.getElementById('first-name');
  9.     lastName = document.getElementById('last-name');
  10.     dateIn = document.getElementById('date-in');
  11.     dateOut = document.getElementById('date-out');
  12.     people = document.getElementById('people-count');
  13.  
  14.     reservationInfo = document.querySelector('.info-list');
  15.     confirmReservation = document.querySelector('.confirm-list');
  16.  
  17.     nextBtn.addEventListener('click', nextHandler);
  18.     function nextHandler(e) {
  19.         e.preventDefault()
  20.         if ((firstName.value == '' ||
  21.             lastName.value == '' ||
  22.             dateIn.value == '' ||
  23.             dateOut.value == '' ||
  24.             people.value == '')) {
  25.             console.log('fail')
  26.             return;
  27.         }
  28.         let x = new Date(dateIn.value);
  29.         let y = new Date(dateOut.value);
  30.         if (x >= y) {
  31.             return;
  32.         }
  33.  
  34.         let SavedData = {
  35.             firstName: firstName.value,
  36.             lastName: lastName.value,
  37.             dateIn: dateIn.value,
  38.             dateOut: dateOut.value,
  39.             people: people.value,
  40.         };
  41.         let newLi = document.createElement('li');
  42.         newLi.setAttribute('class', 'reservation-content');
  43.  
  44.         let newArticle = document.createElement('article');
  45.         let h3 = document.createElement('h3');
  46.         h3.textContent = `Name: ${firstName.value} ${lastName.value}`;
  47.         let fromDateP = document.createElement('p');
  48.         fromDateP.textContent = `From date: ${dateIn.value}`;
  49.         let toDateP = document.createElement('p');
  50.         toDateP.textContent = `To date: ${dateOut.value}`;
  51.         let peopleP = document.createElement('p');
  52.         peopleP.textContent = `For ${people.value} people`;
  53.  
  54.         newArticle.appendChild(h3);
  55.         newArticle.appendChild(fromDateP);
  56.         newArticle.appendChild(toDateP);
  57.         newArticle.appendChild(peopleP);
  58.  
  59.         newLi.appendChild(newArticle)
  60.  
  61.         let editBtn = document.createElement('button');
  62.         editBtn.setAttribute('class', 'edit-btn');
  63.         editBtn.textContent = 'Edit';
  64.         let continueBtn = document.createElement('button');
  65.         continueBtn.setAttribute('class', 'continue-btn');
  66.         continueBtn.textContent = 'Continue';
  67.  
  68.         newLi.appendChild(editBtn);
  69.         newLi.appendChild(continueBtn);
  70.         reservationInfo.appendChild(newLi);
  71.  
  72.         firstName.value = '';
  73.         lastName.value = '';
  74.         dateIn.value = '';
  75.         dateOut.value = '';
  76.         people.value = '';
  77.         nextBtn.disabled = true;
  78.  
  79.         editBtn.addEventListener('click', editHandler);
  80.         continueBtn.addEventListener('click', continueHandler);
  81.  
  82.         function editHandler(e) {
  83.             console.log('edit')
  84.             firstName.value = SavedData['firstName'];
  85.             lastName.value = SavedData['lastName'];
  86.             dateIn.value = SavedData['dateIn'];
  87.             dateOut.value = SavedData['dateOut'];
  88.             people.value = SavedData['people'];
  89.             nextBtn.disabled = false;
  90.             reservationInfo.innerHTML = '';
  91.         }
  92.  
  93.         function continueHandler() {
  94.             console.log('continue')
  95.             reservationInfo.innerHTML = '';
  96.  
  97.             confirmReservation.appendChild(newLi);
  98.             editBtn.classList.remove('edit-btn');
  99.             editBtn.setAttribute('class', 'confirm-btn');
  100.             editBtn.textContent = 'Confirm'
  101.  
  102.             continueBtn.classList.remove('continue-btn');
  103.             continueBtn.setAttribute('class', 'cancel-btn');
  104.             continueBtn.textContent = 'Cancel';
  105.  
  106.             editBtn.removeEventListener('click', editHandler)
  107.             continueBtn.removeEventListener('click', continueHandler)
  108.  
  109.             editBtn.addEventListener('click', confirmHandler);
  110.             continueBtn.addEventListener('click', cancelHandler);
  111.         }
  112.  
  113.         let h1 = document.getElementById('verification');
  114.  
  115.         function confirmHandler() {
  116.             nextBtn.disabled = false;
  117.             h1.textContent = 'Confirmed.'
  118.             h1.setAttribute('class', 'reservation-confirmed');
  119.             confirmReservation.remove();
  120.         }
  121.         function cancelHandler() {
  122.             nextBtn.disabled = false;
  123.             h1.textContent = 'Cancelled.'
  124.             h1.setAttribute('class', 'reservation-cancelled');
  125.             confirmReservation.remove();
  126.         }
  127.  
  128.     }
  129.  
  130. }
  131.  
  132.  
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement