Advertisement
Guest User

Untitled

a guest
May 20th, 2020
210
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.  
  4.   while (input.length > 0) {
  5.     let commands = input.shift().split(' ');
  6.     let singleCommand = commands[0];
  7.  
  8.     switch (singleCommand) {
  9.       case 'Remove':
  10.         let index = +commands[1];
  11.         if (index < listArr.length) {
  12.           listArr.splice(index, 1);
  13.         }
  14.         break;
  15.  
  16.       case 'Add':
  17.         let name = commands[1];
  18.         let index2 = +commands[2];
  19.  
  20.         if (listArr.indexOf(name) === -1) {
  21.           listArr.push(name);
  22.         } else if (index2 < listArr.length || index2 === 0) {
  23.           listArr.splice(index2, 0, name);
  24.         }
  25.         break;
  26.  
  27.       case 'Export':
  28.         let startIndex = +commands[1];
  29.         let count = +commands[2];
  30.  
  31.         if (startIndex < listArr.length) {
  32.           console.log(`${listArr.slice(startIndex, startIndex + count).join(' ')}`);
  33.         }
  34.         break;
  35.  
  36.       case 'Print':
  37.         let command = commands[1];
  38.  
  39.         if (command === 'Reversed') {
  40.           listArr.reverse();
  41.         }
  42.         console.log(`Contacts: ${listArr.join(' ')}`);
  43.         return;
  44.     }
  45.   }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement