Advertisement
EntropyStarRover

Technology Fundamentals Retake Mid Exam - 16 April 2019

Jun 25th, 2019
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  03. Easter Shopping
  2.  
  3.  
  4. function easterShopping(input) {
  5.     shopsArr = input.shift().split(" ");
  6.     let commandsArr = input.slice(1);
  7.  
  8.  
  9.  
  10.     for (let i = 0; i < commandsArr.length; i++) {
  11.         let currentAction = commandsArr[i].split(" ");
  12.         let action = currentAction[0];
  13.  
  14.         if (action.includes("Include")) {
  15.             let newShop = currentAction[1];
  16.             shopsArr.push(newShop);
  17.  
  18.  
  19.         } else if (action.includes("Visit")) {
  20.             let numberOfShopsToRemove = Number(currentAction[2]);
  21.  
  22.             if (shopsArr.length >= numberOfShopsToRemove) {
  23.                 if (currentAction.includes("first")) {
  24.                     shopsArr.splice(0, numberOfShopsToRemove);
  25.  
  26.  
  27.                 } else if (currentAction.includes("last")) {
  28.                     for (let j = 0; j < numberOfShopsToRemove; j++) {
  29.                         shopsArr.pop();
  30.                     }
  31.  
  32.                 }
  33.  
  34.             }
  35.         } else if (action.includes("Prefer")) {
  36.             let indexOfFirstShop = Number(currentAction[1]);
  37.             let indexOfSecondShop = Number(currentAction[2]);
  38.             if (indexOfFirstShop >= 0 && indexOfFirstShop < shopsArr.length && indexOfSecondShop >= 0 &&
  39.                 indexOfSecondShop < shopsArr.length) {
  40.                 let firstShop = shopsArr[indexOfFirstShop];
  41.                 let secondShop = shopsArr[indexOfSecondShop];
  42.                 shopsArr.splice(indexOfFirstShop, 1, secondShop);
  43.                 shopsArr.splice(indexOfSecondShop, 1, firstShop);
  44.  
  45.             }
  46.         } else if (action.includes("Place")) {
  47.             let indexToPlaceAfter = Number(currentAction[2]);
  48.             let indexToPlaceAt = indexToPlaceAfter + 1;
  49.  
  50.             if (indexToPlaceAt >= 0 && indexToPlaceAt < shopsArr.length) {
  51.                 shopsArr.splice(indexToPlaceAt, 0, currentAction[1]);
  52.             }
  53.         }
  54.  
  55.     }
  56.     console.log("Shops left:")
  57.     console.log(shopsArr.join(" "));
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement