SHARE
TWEET

Untitled

a guest Feb 26th, 2020 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input = []){
  2.  
  3.     let deck = input.shift().split(`:`);
  4.     let newDeck = [];
  5.     console.log(deck.join(` `));
  6.  
  7.     let enter;
  8.  
  9.     while((enter = input.shift()) !== `Ready`){
  10.  
  11.         let [action, firstCard, indexOrSecondCard] = enter.split(` `);
  12.  
  13.         switch(action){
  14.  
  15.             case `Add`:
  16.                 if(deck.includes(firstCard)){
  17.                     newDeck.push(firstCard);
  18.                 }
  19.                 else{
  20.                     console.log('Card not found.');
  21.                 }
  22.                 break;
  23.  
  24.             case `Insert`:
  25.                 let index = Number(indexOrSecondCard)
  26.                 if(newDeck.includes(firstCard) && (index > -1 && index < newDeck.length)){
  27.                     newDeck.splice(index, 0, firstCard);
  28.                 }
  29.                 else{
  30.                     console.log(`Error!`);
  31.                 }
  32.                 break;
  33.  
  34.             case `Swap`:
  35.                 let secondCard = indexOrSecondCard;
  36.                 if(newDeck.includes(firstCard) && newDeck.includes(secondCard)){
  37.                     let firstIndex = newDeck.indexOf(firstCard);
  38.                     let secondIndex = newDeck.indexOf(secondCard);
  39.  
  40.                     newDeck[firstIndex] = secondCard;
  41.                     newDeck[secondIndex] = firstCard;
  42.                 }
  43.                 break;
  44.  
  45.             case `Remove`:
  46.                 if(deck.includes(firstCard)){
  47.                     let index = newDeck.indexOf(firstCard);
  48.                     if(index != -1){
  49.                         newDeck.splice(index, 1);
  50.                     }
  51.                 }
  52.                 else{
  53.                     console.log('Card not found.');
  54.                 }
  55.                 break;
  56.            
  57.             case `Shuffle deck`:
  58.                 newDeck.reverse();
  59.         }
  60.     }
  61.     console.log(newDeck.join(` `));
  62.  
  63. }
  64.  
  65. solve([`Innervate:Moonfire:Pounce:Claw:Wrath:Bite`,
  66. `Add Moonfire`,
  67. `Add Pounce`,
  68. `Add Bite`,
  69. `Add Wrath`,
  70. `Insert Claw 0`,
  71. `Swap Claw Moonfire`,
  72. `Remove Bite`,
  73. `Shuffle deck`,
  74. `Ready`
  75. ])
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