Advertisement
-Enigmos-

coffeeLover.js

Feb 20th, 2022
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function coffeeLover(input) {
  2.     let index = 0;
  3.     let coffeeList = input[index].split(` `);
  4.     index++;
  5.     let cmdCounter = Number(input[index]);
  6.     index++;
  7.  
  8.     for (let i = 0; i < cmdCounter; i++) {
  9.         let cmdList = input[index].split(` `);
  10.         index++;
  11.         let cmd = cmdList[0];
  12.         let value = cmdList[1];
  13.  
  14.         switch(cmd) {
  15.             case "Include":
  16.                 coffeeList.push(value);
  17.                 break;
  18.             case "Remove":
  19.                 if (value === "first") {
  20.                     coffeeList.splice(0, Number(cmdList[2]));
  21.                 } else if (value === "last") {
  22.                     coffeeList.splice(-1, Number(cmdList[2]));
  23.                 }
  24.                 break;
  25.             case "Prefer":
  26.                 if (coffeeList[Number(value)] !== undefined && coffeeList[Number(cmdList[2])] !== undefined) {
  27.                     let index = Number(value);
  28.                     let index2 = Number(cmdList[2]);
  29.                     let indexBackup = coffeeList[index2];
  30.                     coffeeList[index2] = coffeeList[index];
  31.                     coffeeList[index] = indexBackup;
  32.                 }
  33.                 break;
  34.             case "Reverse":
  35.                 coffeeList = coffeeList.reverse();
  36.                 break;
  37.         }
  38.     }
  39.     console.log("Coffees:");
  40.     console.log(coffeeList.join(` `));
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement