SHARE
TWEET

Untitled

a guest Feb 24th, 2020 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input = []){
  2.  
  3.     let squad = input.shift().split(` `);
  4.  
  5.     for(let i = 0; i < input.length; i++){
  6.  
  7.         let [command, name, index] = input[i].split(` `);
  8.         let subCommand = name;
  9.        
  10.         switch(command){
  11.             case `Join`: join(name); break;
  12.             case `Jump`: jump(name, index); break;
  13.             case `Dive`: dive(index); break;
  14.             case `First`: first(subCommand); break;
  15.             case `Last`: last(subCommand); break;
  16.             case `Print`: print(subCommand); break;
  17.         }
  18.     }
  19.  
  20.     function join(frog){
  21.         squad.push(frog);
  22.     }
  23.  
  24.     function jump(frog, index){
  25.         squad.splice(index, 0, frog);
  26.     }
  27.  
  28.     function dive(index){
  29.         const isThereFromOnThatIndex = squad[index] !== undefined;
  30.  
  31.         if(isThereFromOnThatIndex){
  32.             squad.splice(index,1)
  33.         }
  34.     }
  35.  
  36.     function first(subCommand){
  37.         if(subCommand > squad.length){
  38.             console.log(squad.join(` `));
  39.         }
  40.         else{
  41.             let firsts = squad.filter((i, index) => (index < subCommand))
  42.             console.log(firsts.join(` `));
  43.         }
  44.     }
  45.     function last(subCommand){
  46.         if(subCommand > squad.length){
  47.             console.log(squad.join(` `));
  48.         }
  49.         else{
  50.             console.log(squad.slice(Math.max(squad.length - subCommand, 0)));
  51.         }
  52.     }
  53.  
  54.     function print(subCommand){
  55.         if(subCommand === `Normal`){
  56.             console.log(`Frogs: ${squad.join(` `)}`);
  57.         }
  58.         else if(subCommand === `Reversed`){
  59.             let reversed = squad.reverse()
  60.             console.log(`Frogs: ${reversed.join(` `)}`);
  61.         }
  62.    
  63.     }
  64. }
  65.  
  66. solve([`Blake Muggy Kishko`,
  67. `Join Kvachko`,
  68. `Dive 0`,
  69. `First 10`,
  70. `Print Reversed`
  71. ])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top