Advertisement
Guest User

LastStop

a guest
Nov 4th, 2020
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function lastStop(input) {
  2.   let paintingNums = input.shift().split(" ").map(Number);
  3.   let commandLine = input.shift();
  4.  
  5.   while (commandLine !== "END") {
  6.     let [command, arg1, arg2] = commandLine.split(" ");
  7.     arg1 = Number(arg1);
  8.     arg2 = Number(arg2);
  9.     if (command === "Insert") {
  10.       let index = arg1;
  11.       if (index > -1 && index <= paintingNums.length - 1) {
  12.         paintingNums.splice(index + 1, 0, arg2);
  13.       }
  14.     } else if (command === "Switch") {
  15.       let index1 = paintingNums.indexOf(arg1);
  16.       let index2 = paintingNums.indexOf(arg2);
  17.       if (index1 > -1 && index2 > -1) {
  18.         let temp = paintingNums[index1];
  19.         paintingNums[index1] = paintingNums[index2];
  20.         paintingNums[index2] = temp;
  21.       }
  22.     } else if (command === "Hide") {
  23.       let index = paintingNums.indexOf(arg1);
  24.       if (index > -1) {
  25.         paintingNums.splice(index, 1);
  26.       }
  27.     } else if (command === "Reverse") {
  28.       paintingNums = paintingNums.reverse();
  29.     } else if (command === "Change") {
  30.       let index = paintingNums.indexOf(arg1);
  31.       if (index > -1) {
  32.         paintingNums[index] = arg2;
  33.       }
  34.     }
  35.  
  36.     commandLine = input.shift();
  37.   }
  38.  
  39.   console.log(paintingNums.join(" "));
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement