Advertisement
nikolapetkov824

Face Ctrl

Jun 8th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getData() {
  2.     let textarea=JSON.parse(document.querySelector('textarea').value);
  3.     let peopleInParagraph=document.querySelector('#peopleIn p');
  4.     let peopleOutParagraph=document.querySelector('#peopleOut p');
  5.     let blacklistParagraph=document.querySelector('#blacklist p');
  6.     let lastElement=textarea.pop();
  7.  
  8.     let peopleIn=[];
  9.     let peopleOut=[];
  10.     let blacklist=[];
  11.  
  12.     for (const person of textarea) {
  13.         let action=person.action;
  14.         let currentPerson={
  15.             firstName:person.firstName,
  16.             lastName:person.lastName
  17.         }
  18.  
  19.  
  20.         if (action==='peopleIn') {
  21.             if (!blacklist.find(p=>p.firstName===currentPerson.firstName &&
  22.                 p.lastName===currentPerson.lastName)) {
  23.                
  24.                     peopleIn.push(currentPerson);
  25.             }
  26.         }else if (action==='peopleOut') {
  27.             if (peopleIn.find(p=>p.firstName===currentPerson.firstName &&
  28.                 p.lastName===currentPerson.lastName)) {
  29.                
  30.                     let index=peopleIn.findIndex(p=>p.firstName===currentPerson.firstName &&
  31.                         p.lastName===currentPerson.lastName);
  32.                     peopleIn.splice(index,1);
  33.                     peopleOut.push(currentPerson);
  34.             }
  35.            
  36.         }else if (action==='blacklist') {
  37.             if (peopleIn.find(p=>p.firstName===currentPerson.firstName &&
  38.                 p.lastName===currentPerson.lastName)) {
  39.                
  40.                     let index=peopleIn.findIndex(p=>p.firstName===currentPerson.firstName &&
  41.                         p.lastName===currentPerson.lastName);
  42.                     peopleIn.splice(index,1);
  43.                     peopleOut.push(currentPerson);
  44.             }
  45.                 blacklist.push(currentPerson);
  46.         }
  47.     }
  48.  
  49.     let output={};
  50.     output['peopleIn']=peopleIn;
  51.  
  52.     output['peopleOut']=peopleOut;
  53.  
  54.     output['blacklist']=blacklist;
  55.  
  56.     if (lastElement.action!=='' &&
  57.     lastElement.criteria!=='') {
  58.         let criteria=lastElement.criteria;
  59.         output[lastElement.action]=output[lastElement.action]
  60.         .sort((a,b)=>a[criteria].localeCompare(b[criteria]));
  61.     }
  62.  
  63.     peopleInParagraph.textContent=output.peopleIn
  64.     .map(x=>JSON.stringify(x))
  65.     .join(' ');
  66.  
  67.     peopleOutParagraph.textContent=output.peopleOut
  68.     .map(x=>JSON.stringify(x))
  69.     .join(' ');
  70.  
  71.     blacklistParagraph.textContent=output.blacklist
  72.     .map(x=>JSON.stringify(x))
  73.     .join(' ');
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement