Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input = []) {
- let shopList = input.shift().split(' ');
- let commandCount = Number(input.shift());
- for (let i = 1; i <= commandCount; i++) {
- let commands = input.shift().split(' ')
- switch (commands[0]) {
- case 'Include':
- shopList.push(commands[1])
- break;
- case 'Visit':
- let firstOrLast = commands[1];
- let numOfDeletes = Number(commands[2]);
- if (shopList.length > numOfDeletes) {
- switch (firstOrLast) {
- case 'first':
- for (let i = 1; i <= numOfDeletes; i++) {
- shopList.shift()
- }
- break;
- case 'last':
- for (let i = 1; i <= numOfDeletes; i++) {
- shopList.pop()
- }
- break;
- }
- }
- break;
- case 'Prefer':
- let firstIndex = +commands[1];
- let secondIndex = +commands[2];
- if (shopList.length > firstIndex && shopList.length > secondIndex) {
- let indexEl = shopList[firstIndex];
- let secIndexEl = shopList[secondIndex];
- shopList.splice(firstIndex, 1, secIndexEl);
- shopList.splice(secondIndex, 1, indexEl)
- }
- break;
- case 'Place':
- let shop = commands[1];
- let index = +commands[2];
- if (shopList.length > index + 1) {
- shopList.splice(index + 1, 0, shop)
- }
- }
- }
- console.log('Shops left:');
- console.log(shopList.join(' '));
- }
- solve([
- 'Bershka CandyStore ThriftShop Armani Groceries ToyStore PeakStore',
- '5',
- 'Include HM',
- 'Visit first 2',
- 'Visit last 1',
- 'Prefer 3 1',
- 'Place Library 2'
- ]
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement