Todorov_Stanimir

03. Last Stop Technology FundamMid Exam - 10 March 2019

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