Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr){
  2.     let contacts = arr.shift().split(' ');
  3.     for(let i = 0; i < arr.length; i++){
  4.         let [cmd, second, third] = arr[i].split(' ');
  5.         if(cmd === 'Add'){
  6.             if(!contacts.includes(second)){
  7.                 contacts.push(second);
  8.             }else{
  9.                 if(0 <= +(third) && +(third) < contacts.length){
  10.                     contacts.splice(+(third), 0, second);
  11.                 }
  12.             }
  13.         }
  14.         if(cmd === 'Remove'){
  15.             if(0 <= +(second) && +(second) < contacts.length){
  16.                 contacts.splice(+(second), 1);
  17.             }
  18.         }
  19.         if(cmd === 'Export'){
  20.             let exported = [];
  21.             exported = contacts.slice(+(second), +(third)||contacts.length);
  22.             console.log(exported.join(' '));
  23.         }
  24.         if(cmd === 'Print' && second === 'Normal'){
  25.             console.log(`Contacts: ${contacts.join(' ')}`)
  26.             return;
  27.         }
  28.         if(cmd === 'Print' && second === 'Reversed'){
  29.             console.log(`Contacts: ${contacts.reverse().join(' ')}`);
  30.             return;
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement