Advertisement
Guest User

Untitled

a guest
May 27th, 2019
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function easterShoppings(inputArray) {
  2.     let shops = inputArray.shift().split(' ');
  3.     const commandsCount = Number(inputArray.shift());
  4.  
  5.     for (let i = 0; i < commandsCount; i += 1) {
  6.         const currentCommand = inputArray[i].split(' ');
  7.  
  8.         switch (currentCommand[0]) {
  9.             case 'Include':
  10.                 shops.push(currentCommand[1]);
  11.                 break;
  12.             case 'Visit':
  13.                 const index = +currentCommand[2];
  14.  
  15.                 if (index <= shops.length && index >= 0) {// Проверка на индексите
  16.                     if (currentCommand[1] === 'first') {
  17.                         shops.splice(0, index)
  18.                     } else {
  19.                         shops.splice((shops.length - index), (index))// Не трябва да е shops.splice((shops.length - index), (shops.length - index))
  20.                     }
  21.                 }
  22.                 break;
  23.             case 'Prefer':
  24.                 const firstIndex = +currentCommand[1];
  25.                 const secondIndex = +currentCommand[2];
  26.                 if (firstIndex >= 0 && secondIndex >= 0 && shops.length > firstIndex && shops.length > secondIndex) {// Проверка на индексите
  27.                     const tempStoreFirstIndex = shops[firstIndex];
  28.                     shops[firstIndex] = shops[secondIndex]
  29.                     shops[secondIndex] = tempStoreFirstIndex;
  30.                 }
  31.                 break;
  32.             case 'Place':
  33.                 let i = +currentCommand[2];
  34.                 if (shops.length > i + 1 && i >= 0) {// Проверка на индексите
  35.                     shops.splice(i + 1, 0,  currentCommand[1])
  36.                 }
  37.         }
  38.     }
  39.     console.log('Shops left:');
  40.     console.log(shops.join(' '));
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement