Advertisement
svetlio_top

Weaponsmith

Feb 26th, 2020
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Weapon(input){
  2.     let w = input.shift();
  3.     let weapon = w.split("|");
  4.     let command = input.shift();
  5.     let arrEvenOdd=[];
  6.    
  7.        
  8.         while(command !== 'Done'){
  9.          
  10.                 if(command.includes('Move Right')){
  11.                    
  12.                     let num = command.split(" ");
  13.                  
  14.                     let indexR = +num[2];
  15.                  
  16.                     if (indexR <= weapon.length && indexR >= 0) {
  17.          
  18.                         let val = weapon[indexR];
  19.                            
  20.                             weapon.splice(indexR+2, 0, val);  
  21.                             weapon.splice(indexR, 1);        
  22.                      
  23.                     }
  24.                     command = input.shift();
  25.                 }else if(command.includes('Move Left')){
  26.                    
  27.                     let num = command.split(" ");
  28.                     let indexL = +num[2];
  29.  
  30.                     if (indexL > 0) {
  31.  
  32.                         let val = weapon[indexL];
  33.                         weapon.splice(indexL-1, 0, val);
  34.                         weapon.splice(indexL+1, 1);
  35.            
  36.                     }
  37.                     command = input.shift();
  38.  
  39.                 }else if(command.includes('Odd')){
  40.                     for (let k = 0; k < weapon.length; k++) {
  41.                         if(k % 2 !== 0){
  42.                             arrEvenOdd.push(weapon[k]);
  43.                         }
  44.                     }
  45.                     command = input.shift();
  46.                 }else if(command.includes('Even')){
  47.                     for (let k = 0; k < weapon.length; k++) {
  48.                         if(k % 2 === 0){
  49.                             arrEvenOdd.push(weapon[k]);
  50.                         }
  51.                     }
  52.                     command = input.shift();
  53.  
  54.                 }
  55.            
  56.        
  57.     }
  58.     if(arrEvenOdd.length>0){
  59.     console.log(arrEvenOdd.join(' '));
  60.     }
  61.     console.log(`You crafted ${weapon.join('')}!`);
  62.  
  63. }
  64. Weapon(['ri|As|er|hb|ng',
  65.         'Move Left 1',
  66.         'Move Right 2',
  67.         'Move Right 3',
  68.         'Move Left 2',
  69.         'Done']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement