Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function easterShoping (input) {
- let shopList = input.shift().split(' ')
- let commandsNumber = +input.shift()
- for (let element of input) {
- let [command, shop, number] = element.split(' ')
- if (command === 'Include') {
- shopList.push(shop)
- } else if (command === 'Visit' && input.length >= number) {
- if (shop === 'first') {
- shopList.splice(0, number)
- } else if (shop === 'last') {
- shopList.length -= +number
- }
- } else if (command === 'Prefer') {
- let shopIndex1 = +shop
- let shopIndex2 = +number
- let firstToSwap = shopList[shopIndex1]
- let secondToSwap = shopList[shopIndex2]
- if (
- shopIndex1 >= 0 &&
- shopIndex1 < shopList.length &&
- shopIndex2 >= 0 &&
- shopIndex2 < shopList.length
- ) {
- shopList.splice(shopIndex1, 1, secondToSwap)
- shopList.splice(shopIndex2, 1, firstToSwap)
- }
- } else if (command === 'Place') {
- let shopIndex = +number
- if (shopIndex >= 0 && shopIndex < shopList.length) {
- shopList.splice(shopIndex + 1, 0, shop)
- }
- }
- }
- console.log(`Shops left:`)
- console.log(shopList.join(' '))
- }
- easterShoping([
- 'Boutique Flowers CandyStore ThriftShop Versace Groceries ToyStore PeakStore',
- '6',
- 'Visit first 9',
- 'Visit last 4',
- 'Visit last 1',
- 'Prefer 3 8',
- 'Place Store 7'
- ])
Advertisement
Add Comment
Please, Sign In to add comment