Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function lastStop(input) {
- let collection = input.shift().split(' ').map(Number);
- let currenOperation = input.shift().split(' ');
- let command = currenOperation[0];
- while (command !== 'END') {
- if (command === 'Change') {
- let paintingNumber = Number(currenOperation[1]);
- let changedNumber = Number(currenOperation[2]);
- if (collection.includes(paintingNumber)) {
- collection.splice(collection.indexOf(paintingNumber), 1, changedNumber);
- }
- } else if (command === 'Hide') {
- let numberForHidden = Number(currenOperation[1]);
- if (collection.includes(numberForHidden)) {
- collection.splice(collection.indexOf(numberForHidden), 1);
- }
- } else if (command === 'Switch') {
- let paintingNumber1 = Number(currenOperation[1]);
- let paintingNumber2 = Number(currenOperation[2]);
- let index1 = collection.indexOf(paintingNumber1);
- let index2 = collection.indexOf(paintingNumber2);
- if (collection.includes(paintingNumber1) && collection.includes(paintingNumber2)) {
- collection.splice(index1, 1, paintingNumber2);
- collection.splice(index2, 1, paintingNumber1);
- }
- } else if (command === 'Insert') {
- let index = Number(currenOperation[1]) + 1;
- let paintingNumber = Number(currenOperation[2]);
- if (0 <= index && index <= collection.length) {
- collection.splice(index, 0, paintingNumber)
- }
- } else if (command === 'Reverse') {
- collection.reverse();
- }
- currenOperation = input.shift().split(' ');
- command = currenOperation[0];
- }
- console.log(collection.join(' '));
- }
Advertisement
Add Comment
Please, Sign In to add comment