Advertisement
GeorgiLukanov87

01. Work Process - Js Advanced Final Exam - 19 February 2022

Mar 23rd, 2023 (edited)
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 01. Work Process
  2. // Js Advanced Final Exam - 19 February 2022
  3. // https://judge.softuni.org/Contests/Practice/Index/3367#0
  4.  
  5. function solve() {
  6.     const fnameElement = document.getElementById('fname');
  7.     const lnameElement = document.getElementById('lname');
  8.     const emailElement = document.getElementById('email');
  9.     const birthElement = document.getElementById('birth');
  10.     const positionElement = document.getElementById('position');
  11.     const salaryElement = document.getElementById('salary');
  12.  
  13.     const tableElement = document.getElementById('tbody');
  14.     const budgetElement = document.getElementById('sum');
  15.     document.getElementById('add-worker').addEventListener('click', hireWorker);
  16.     let totalSalary = [];
  17.  
  18.     function hireWorker(e) {
  19.         e.preventDefault();
  20.         let DataSaved = {
  21.             fname: fnameElement.value, lname: lnameElement.value,
  22.             email: emailElement.value, birth: birthElement.value,
  23.             position: positionElement.value, salary: salaryElement.value,
  24.         }
  25.         if (DataSaved.fname == '' || DataSaved.lname == '' || DataSaved.email == '' ||
  26.             DataSaved.birth == '' || DataSaved.position == '' || DataSaved.salary == '') {
  27.             alert('Fill all inputs')
  28.             return;
  29.         }
  30.  
  31.         let tr = document.createElement('tr');
  32.         createCustomEl('td', DataSaved.fname, tr); createCustomEl('td', DataSaved.lname, tr);
  33.         createCustomEl('td', DataSaved.email, tr); createCustomEl('td', DataSaved.birth, tr);
  34.         createCustomEl('td', DataSaved.position, tr); createCustomEl('td', DataSaved.salary, tr);
  35.         let btnsContainer = document.createElement('td');
  36.  
  37.         let firedBtn = document.createElement('button');
  38.         firedBtn.textContent = 'Fired';
  39.         firedBtn.classList.add('fired');
  40.         firedBtn.addEventListener('click', fireWorker)
  41.  
  42.         let editBtn = document.createElement('button')
  43.         editBtn.textContent = 'Edit';
  44.         editBtn.classList.add('edit');
  45.         editBtn.addEventListener('click', editInfo)
  46.  
  47.         btnsContainer.appendChild(firedBtn);
  48.         btnsContainer.appendChild(editBtn);
  49.         tr.appendChild(btnsContainer);
  50.         tableElement.appendChild(tr);
  51.  
  52.         totalSalary.push(Number(DataSaved.salary))
  53.         displaySalary();
  54.         clearInputs();
  55.     }
  56.  
  57.     function editInfo(e) {
  58.         currentWorker = e.target.parentNode.parentNode;
  59.         let editSalary = currentWorker.children[5].textContent;
  60.  
  61.         fnameElement.value = currentWorker.children[0].textContent;
  62.         lnameElement.value = currentWorker.children[1].textContent;
  63.         emailElement.value = currentWorker.children[2].textContent;
  64.         birthElement.value = currentWorker.children[3].textContent;
  65.         positionElement.value = currentWorker.children[4].textContent;
  66.         salaryElement.value = currentWorker.children[5].textContent;
  67.  
  68.         editBudget(editSalary)
  69.         displaySalary();
  70.         currentWorker.remove();
  71.     }
  72.  
  73.     function displaySalary() {
  74.         let totalSum = totalSalary.reduce((a, b) => a + b, 0)
  75.         budgetElement.textContent = totalSum.toFixed(2);
  76.     }
  77.  
  78.     function fireWorker(e) {
  79.         currentWorker = e.target.parentNode.parentNode;
  80.         let editSalary = currentWorker.children[5].textContent;
  81.         editBudget(editSalary)
  82.         displaySalary();
  83.         currentWorker.remove();
  84.         alert(`Worker with name: "${currentWorker.children[0].textContent} ${currentWorker.children[1].textContent}" was FIRED!`)
  85.     }
  86.  
  87.     function editBudget(editSalary) {
  88.         let index = totalSalary.indexOf(Number(editSalary));
  89.         totalSalary.splice(index, 1);
  90.     }
  91.  
  92.     function clearInputs() {
  93.         fnameElement.value = ''; lnameElement.value = ''; emailElement.value = '';
  94.         birthElement.value = ''; positionElement.value = ''; salaryElement.value = '';
  95.     }
  96.     function createCustomEl(type, text, parent, className) {
  97.         let newElement = document.createElement(type);
  98.         newElement.textContent = text;
  99.         if (className) {
  100.             newElement.classList.add(className);
  101.         }
  102.         parent.appendChild(newElement);
  103.     }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement