Ivankooo1

Weaponsmith

Dec 3rd, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr){
  2.    let weaponName = arr.shift().split('|');
  3.    let el = '';
  4.    for(let element of arr){
  5.  
  6.        if(element === 'Done'){
  7.          break;
  8.        }
  9.        let allCommand = element.split(' ');// Move Right 0
  10.        let command = allCommand.shift();// Move
  11.        let position = allCommand.shift();// Right
  12.  
  13.        switch(command){
  14.          case 'Move':  
  15.                         let index = Number(allCommand);
  16.                         switch(position){
  17.                             case 'Right':
  18.                                 if(index < 0 || index >= weaponName.length){
  19.                                    break;
  20.                                 }else if(index >= 0 && index < weaponName.length - 1){
  21.                                   [weaponName[index + 1], weaponName[index]] = [weaponName[index], weaponName[index + 1]];
  22.                                 }
  23.                                                            
  24.                             ;break;
  25.  
  26.                             case 'Left':
  27.                                 if(index < 0 || index >= weaponName.length){
  28.                                   break;
  29.                                 }else if(index > 0 && index < weaponName.length){
  30.                                   [weaponName[index - 1], weaponName[index]] = [weaponName[index], weaponName[index - 1]]
  31.                                 }
  32.                             ;break;
  33.                       };break;
  34.          case 'Check': switch(position){
  35.                             case 'Even':
  36.                               console.log(weaponName.filter((el,index)=> index % 2 === 0).join(' '));
  37.                               ;break;
  38.                             case 'Odd':
  39.                               console.log(weaponName.filter((el, index) => index % 2 !== 0).join(' '));
  40.                               ;break;
  41.                       };break;
  42.        }
  43.  
  44.  
  45.    }
  46.    
  47.    console.log(weaponName.join(''))
  48.        
  49. }
  50.  
  51. solve([
  52.     'ha|Do|mm|om|er',
  53.     'Move Right 0',
  54.     'Move Left 3',
  55.     'Check Odd',
  56.     'Move Left 2',
  57.     'Move Left 10',
  58.     'Move Left 0',
  59.     'Done'
  60. ]);
Advertisement
Add Comment
Please, Sign In to add comment