Advertisement
Guest User

ContactList

a guest
May 21st, 2020
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let listArr = input.shift().split(' ');
  3.     while (input.length > 0) {
  4.         let commands = input.shift().split(' ');
  5.         let singleCommand = commands[0];
  6.         switch (singleCommand) {
  7.             case 'Remove':
  8.                 let index = +commands[1];
  9.                 if (index >= 0 && index < listArr.length) {
  10.                     listArr.splice(index, 1);
  11.                 }
  12.                 break;
  13.             case 'Add':
  14.                 let name = commands[1];
  15.                 let index2 = +commands[2];
  16.                 if (listArr.indexOf(name) === -1) {
  17.                     listArr.push(name);
  18.                 } else if (index2 >= 0 && index2 <= listArr.length) {
  19.                     listArr.splice(index2, 0, name);
  20.                 }
  21.                 break;
  22.             case 'Export':
  23.                 let startIndex = +commands[1];
  24.                 let count = +commands[2];
  25.                 if (startIndex < listArr.length) {
  26.                     console.log(`${listArr.slice(startIndex, startIndex + count).join(' ')}`);
  27.                 }
  28.                 break;
  29.             case 'Print':
  30.                 let command = commands[1];
  31.                 if (command === 'Reversed') {
  32.                     listArr.reverse();
  33.                 }
  34.                 console.log(`Contacts: ${listArr.join(' ')}`);
  35.                 return;
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement