Advertisement
Guest User

froggySquad

a guest
Oct 29th, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function froggySquad(arr) {
  2.     let output = '';
  3.     let names = arr.shift().split(' ');
  4.     for (let iterator of arr) {
  5.         let command = iterator.split(' ');
  6.         let firstWord = command.shift();
  7.         let secondWord = command.shift();
  8.  
  9.         if (firstWord === 'Join') {
  10.             names.push(secondWord);
  11.         } else if (firstWord === 'Jump') {
  12.             let num = command.shift();
  13.             if (num >= 0 && num <= names.length) {
  14.                 names.splice(Number(num), 0, secondWord);
  15.             }
  16.         } else if (firstWord === 'Dive') {
  17.             let num = Number(secondWord);
  18.             if (num >= 0 && num < names.length) {
  19.                 names.splice(num, 1);
  20.             }
  21.         } else if (firstWord === 'First') {
  22.             let count = Number(secondWord);
  23.             console.log(names.slice(0, count).join(' '));
  24.  
  25.         } else if (firstWord === 'Last') {
  26.             let count = Number(secondWord);
  27.             console.log(names.slice(-count).join(' '));
  28.  
  29.         } else if (firstWord === 'Print') {
  30.             if (secondWord === 'Reversed') {
  31.                 output = `Frogs: ${names.reverse().join(' ')}`;
  32.             } else if (secondWord === 'Normal') {
  33.                 output = `Frogs: ${names.join(' ')}`;
  34.             }
  35.             console.log(output);
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement