Advertisement
Guest User

Untitled

a guest
May 19th, 2020
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input = []) {
  2.     let shopList = input.shift().split(' ');
  3.     let commandCount = Number(input.shift());
  4.  
  5.     for (let i = 1; i <= commandCount; i++) {
  6.         let commands = input.shift().split(' ')
  7.  
  8.         switch (commands[0]) {
  9.             case 'Include':
  10.                 shopList.push(commands[1])
  11.                 break;
  12.             case 'Visit':
  13.                 let firstOrLast = commands[1];
  14.                 let numOfDeletes = Number(commands[2]);
  15.  
  16.                 if (shopList.length > numOfDeletes) {
  17.  
  18.                     switch (firstOrLast) {
  19.                         case 'first':
  20.                             for (let i = 1; i <= numOfDeletes; i++) {
  21.                                 shopList.shift()
  22.                             }
  23.                             break;
  24.  
  25.                         case 'last':
  26.                             for (let i = 1; i <= numOfDeletes; i++) {
  27.                                 shopList.pop()
  28.                             }
  29.                             break;
  30.                     }
  31.                 }
  32.                 break;
  33.  
  34.             case 'Prefer':
  35.                 let firstIndex = +commands[1];
  36.                 let secondIndex = +commands[2];
  37.  
  38.                 if (shopList.length > firstIndex && shopList.length > secondIndex) {
  39.                     let indexEl = shopList[firstIndex];
  40.                     let secIndexEl = shopList[secondIndex];
  41.                     shopList.splice(firstIndex, 1, secIndexEl);
  42.                     shopList.splice(secondIndex, 1, indexEl)
  43.                 }
  44.                 break;
  45.  
  46.             case 'Place':
  47.                 let shop = commands[1];
  48.                 let index = +commands[2];
  49.  
  50.                 if (shopList.length > index + 1) {
  51.                     shopList.splice(index + 1, 0, shop)
  52.                 }
  53.         }
  54.  
  55.  
  56.     }
  57.     console.log('Shops left:');
  58.     console.log(shopList.join(' '));
  59. }
  60.  
  61.  
  62. solve([
  63.     'Bershka CandyStore ThriftShop Armani Groceries ToyStore PeakStore',
  64.     '5',
  65.     'Include HM',
  66.     'Visit first 2',
  67.     'Visit last 1',
  68.     'Prefer 3 1',
  69.     'Place Library 2'
  70. ]
  71. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement