Advertisement
Guest User

Untitled

a guest
Sep 27th, 2019
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function contactList(array) {
  2.  
  3.     let conList = array.shift().split(' ');
  4.  
  5.     let exportArr = [];
  6.     let exportArr2 = [];
  7.  
  8.     while (true) {
  9.         let command = array.shift().split(' ');
  10.  
  11.         let firstWord = command.shift();
  12.         let secondWord = command.shift();
  13.  
  14.         if (firstWord === 'Add') {
  15.             let num = command.shift();
  16.             if (conList.includes(secondWord)) {
  17.                 if (num >= 0 && num <= conList.length) {
  18.                     conList.splice(Number(num), 0, secondWord)
  19.                 }
  20.             } else {
  21.                 conList.push(secondWord);
  22.             }
  23.  
  24.         } else if (firstWord === 'Remove') {
  25.             let num = Number(secondWord);
  26.             if (num >= 0 && num <= conList.length) {
  27.                 conList.splice(num, 1)
  28.             }
  29.         } else if (firstWord === 'Export') {
  30.             if (Number(command) >= conList.length && Number(command) > 0) {
  31.                 exportArr2 = conList.slice(Number(secondWord), conList.length);
  32.                 console.log(exportArr2.join(' '));
  33.  
  34.             } else if (Number(command) <= conList.length && Number(command) > 0) {
  35.                 exportArr = conList.slice(Number(secondWord), Number(command));
  36.                 console.log(exportArr.join(' '));
  37.             }
  38.         } else if (secondWord === 'Normal') {
  39.             console.log(`Contacts: ${conList.join(' ')}`);
  40.             break;
  41.         } else if (secondWord === 'Reversed') {
  42.             console.log(`Contacts: ${conList.reverse().join(' ')}`);
  43.             break;
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement