maymunskoa

04. Party Time (Assoc Arrays)

Mar 6th, 2020
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.   const isVip = (guestName)=>{
  3.         return guestName[0].charCodeAt() >= 48 && guestName[0].charCodeAt(0) <= 57;
  4.   }
  5.    
  6.     let guests = new Map();
  7.     let partyIndex = input.indexOf('PARTY');
  8.     let guestList = input.slice(0,partyIndex);
  9.     let visitors = input.slice(partyIndex+1);
  10.  
  11.     for (const visitor of visitors) {
  12.         guests.set(visitor, 'VISITED');
  13.     }
  14.  
  15.  
  16.  
  17.     for (const guest of guestList) {
  18.        
  19.         let value = isVip(guest)? 'VIP':'REGULAR';
  20.         if(!guests.has(guest)){
  21.             guests.set(guest, value);
  22.         }else{
  23.             guests.delete(guest);
  24.         }
  25.     }  
  26.  
  27.         console.log(Array.from(guests).length);
  28.             Array.from(guests).forEach(pair=>{
  29.                 if(pair[1] === 'VIP'){
  30.                     console.log(pair[0]);
  31.                 }
  32.             });
  33.             Array.from(guests).forEach(pair=>{
  34.                 if(pair[1] === 'REGULAR'){
  35.                     console.log(pair[0]);
  36.                 }
  37.             });
  38.    
  39.    
  40. }
Advertisement
Add Comment
Please, Sign In to add comment