Advertisement
Liliana797979

pet me - js advanced exam

Oct 14th, 2021
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     // get references to elements of interest
  3.     // configure event listeners
  4.     const fields = document.querySelectorAll('#container input');
  5.     const addBtn = document.querySelector('#container button');
  6.     const petList = document.querySelector('#adoption ul');
  7.     const adoptedList = document.querySelector('#adopted ul');
  8.  
  9.     const input = {
  10.         name: fields[0],
  11.         age: fields[1],
  12.         kind: fields[2],
  13.         owner: fields[3],
  14.     }
  15.  
  16.     addBtn.addEventListener('click', addPet);
  17.  
  18.     // # Add new pet
  19.     // Read input fields
  20.     // Validate values
  21.     // Create new list item
  22.     // Configure event listener for newly created element
  23.     function addPet(event) {
  24.         event.preventDefault();
  25.  
  26.         const name = input.name.value.trim();
  27.         const age = Number(input.age.value.trim());
  28.         const kind = input.kind.value.trim();
  29.         const owner = input.owner.value.trim();
  30.  
  31.         if (name == '' || input.age.value.trim() == '' || Number.isNaN(age) || kind == '' || owner == '') {
  32.             return;
  33.         }
  34.  
  35.         const contactBtn = e('button', {}, 'Contact with owner');
  36.  
  37.         const pet = e('li', {},
  38.             e('p', {},
  39.                 e('strong', {}, name),
  40.                 ' is a ',
  41.                 e('strong', {}, age),
  42.                 ' year old ',
  43.                 e('strong', {}, kind)
  44.             ),
  45.             e('span', {}, `Owner: ${owner}`),
  46.             contactBtn
  47.         );
  48.  
  49.         contactBtn.addEventListener('click', contact);
  50.  
  51.         petList.appendChild(pet);
  52.  
  53.         input.name.value = '';
  54.         input.age.value = '';
  55.         input.kind.value = '';
  56.         input.owner.value = '';
  57.  
  58.         // # Contact owner
  59.         // Create confirmation div
  60.         // Configure event listener for new button
  61.         // Replace existing button with confirmation div
  62.         function contact() {
  63.             const confirmInput = e('input', { placeholder: 'Enter your names' });
  64.             const confirmBtn = e('button', {}, 'Yes! I take it!');
  65.             const confirmDiv = e('div', {},
  66.                 confirmInput,
  67.                 confirmBtn
  68.             );
  69.  
  70.             confirmBtn.addEventListener('click', adopt.bind(null, confirmInput, pet));
  71.  
  72.             contactBtn.remove();
  73.             pet.appendChild(confirmDiv);
  74.         }
  75.     }
  76.  
  77.     // # Adopt a pet
  78.     // Read and validate input
  79.     // Create new button for checking
  80.     // Configure event listener for new button
  81.     // Replace confirmation div with new button
  82.     // Change text content of Owner span
  83.     // Move element to other list
  84.     function adopt(input, pet) {
  85.         const newOwner = input.value.trim();
  86.  
  87.         if (newOwner == '') {
  88.             return;
  89.         }
  90.  
  91.         const checkBtn = e('button', {}, 'Checked');
  92.         checkBtn.addEventListener('click', check.bind(null, pet));
  93.  
  94.         pet.querySelector('div').remove();
  95.         pet.appendChild(checkBtn);
  96.  
  97.         pet.querySelector('span').textContent = `New Owner: ${newOwner}`;
  98.  
  99.         adoptedList.appendChild(pet);
  100.     }
  101.  
  102.     // # Checking of adopted pet
  103.     // Remove element from DOM
  104.     function check(pet) {
  105.         pet.remove();
  106.     }
  107.  
  108.     function e(type, attr, ...content) {
  109.         const element = document.createElement(type);
  110.  
  111.         for (let prop in attr) {
  112.             element[prop] = attr[prop];
  113.         }
  114.         for (let item of content) {
  115.             if (typeof item == 'string' || typeof item == 'number') {
  116.                 item = document.createTextNode(item);
  117.             }
  118.             element.appendChild(item);
  119.         }
  120.  
  121.         return element;
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement