Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2019
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.  
  3.     let allPaintingNumbers = input.shift()
  4.         .split(' ')
  5.     //.map(Number);
  6.  
  7.     let instruction = '';
  8.  
  9.     while (true) {
  10.         instruction = input.shift();
  11.  
  12.         if (instruction === 'END') {
  13.             break;
  14.         }
  15.  
  16.         instruction = instruction.split(' '); // an array
  17.         let command = instruction[0];
  18.  
  19.         if (command === 'Change') {
  20.             allPaintingNumbers = change(instruction[1], instruction[2]);
  21.  
  22.         } else if (command === 'Hide') {
  23.             allPaintingNumbers = hide(instruction[1]);
  24.  
  25.         } else if (command === 'Switch') {
  26.             allPaintingNumbers = swap(instruction[1], instruction[2]);
  27.  
  28.         } else if (command === 'Insert') {
  29.             let place = Number(instruction[1]);
  30.             let newPainting = instruction[2];
  31.             place++;
  32.             if (place < allPaintingNumbers.length && place > -1) {
  33.                 allPaintingNumbers.splice(place, 0, newPainting);
  34.             }
  35.  
  36.         } else if (command === 'Reverse') { //reverse
  37.             allPaintingNumbers = allPaintingNumbers.reverse();
  38.         }
  39.  
  40.     }
  41.  
  42.     function change(oldNumber, newNumber) {
  43.         for (let i = 0; i < allPaintingNumbers.length; i++) {
  44.             if (allPaintingNumbers[i] === oldNumber) {
  45.                 allPaintingNumbers[i] = newNumber;
  46.             }
  47.         }
  48.         return allPaintingNumbers;
  49.     }
  50.  
  51.     function hide(paintingNumber) {
  52.         for (let i = 0; i < allPaintingNumbers.length; i++) {
  53.             if (allPaintingNumbers[i] === paintingNumber) {
  54.                 allPaintingNumbers.splice(i, 1);
  55.             }
  56.         }
  57.         return allPaintingNumbers;
  58.     }
  59.  
  60.     function swap(paintingNumberFirst, paintingNumberSecond) {
  61.         let temp;
  62.         let isFirstExist = false;
  63.         let isSecondExist = false;
  64.         let indexFirst;
  65.         let indexSecond;
  66.  
  67.         for (let i = 0; i < allPaintingNumbers.length; i++) {
  68.             if (allPaintingNumbers[i] === paintingNumberFirst) {
  69.                 isFirstExist = true;
  70.                 indexFirst = i;
  71.             }
  72.             if (allPaintingNumbers[i] === paintingNumberSecond) {
  73.                 isSecondExist = true;
  74.                 indexSecond = i;
  75.             }
  76.         }
  77.         if (isFirstExist && isSecondExist) {
  78.             temp = allPaintingNumbers[indexFirst];
  79.             allPaintingNumbers[indexFirst] = allPaintingNumbers[indexSecond];
  80.             allPaintingNumbers[indexSecond] = temp;
  81.         }
  82.         return allPaintingNumbers;
  83.     }
  84.  
  85.     let result = '';
  86.     for (let item of allPaintingNumbers) {
  87.         result += item + ' ';
  88.     }
  89.     console.log(result);
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement