nikolayneykov

Untitled

May 2nd, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function easterShoping (input) {
  2.   let shopList = input.shift().split(' ')
  3.   let commandsNumber = +input.shift()
  4.  
  5.   for (let element of input) {
  6.     let [command, shop, number] = element.split(' ')
  7.  
  8.     if (command === 'Include') {
  9.       shopList.push(shop)
  10.     } else if (command === 'Visit' && input.length >= number) {
  11.       if (shop === 'first') {
  12.         shopList.splice(0, number)
  13.       } else if (shop === 'last') {
  14.         shopList.length -= +number
  15.       }
  16.     } else if (command === 'Prefer') {
  17.       let shopIndex1 = +shop
  18.       let shopIndex2 = +number
  19.       let firstToSwap = shopList[shopIndex1]
  20.       let secondToSwap = shopList[shopIndex2]
  21.  
  22.       if (
  23.         shopIndex1 >= 0 &&
  24.         shopIndex1 < shopList.length &&
  25.         shopIndex2 >= 0 &&
  26.         shopIndex2 < shopList.length
  27.       ) {
  28.         shopList.splice(shopIndex1, 1, secondToSwap)
  29.         shopList.splice(shopIndex2, 1, firstToSwap)
  30.       }
  31.     } else if (command === 'Place') {
  32.       let shopIndex = +number
  33.       if (shopIndex >= 0 && shopIndex < shopList.length) {
  34.         shopList.splice(shopIndex + 1, 0, shop)
  35.       }
  36.     }
  37.   }
  38.  
  39.   console.log(`Shops left:`)
  40.   console.log(shopList.join(' '))
  41. }
  42. easterShoping([
  43.   'Boutique Flowers CandyStore ThriftShop Versace Groceries ToyStore PeakStore',
  44.   '6',
  45.   'Visit first 9',
  46.   'Visit last 4',
  47.   'Visit last 1',
  48.   'Prefer 3 8',
  49.   'Place Store 7'
  50. ])
Advertisement
Add Comment
Please, Sign In to add comment