-Enigmos-

deckOfCards.js

Feb 20th, 2022
715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function deckOfCards(input) {
  2.     let index = 0;
  3.     let cardList = input[index].split(`, `);
  4.     index++;
  5.     let cmdCounter = Number(input[index]);
  6.     index++;
  7.  
  8.     for (let i = 0; i < cmdCounter; i++) {
  9.         let cmdList = input[index].split(`, `);
  10.         index++;
  11.         let cmd = cmdList[0];
  12.         let value = cmdList[1];
  13.  
  14.         if (cmd === "Add" && !cardList.includes(value)) {
  15.             cardList.push(value);
  16.             console.log("Card successfully added");
  17.         } else if (cmd === "Add") {
  18.             console.log("Card is already in the deck");
  19.         }
  20.  
  21.         if (cmd === "Remove" && cardList.includes(value)) {
  22.             let cardIndex = cardList.indexOf(value);
  23.             cardList.splice(cardIndex, 1);
  24.             console.log("Card successfully removed");
  25.         } else if (cmd === "Remove") {
  26.             console.log("Card not found");
  27.         }
  28.  
  29.         if (cmd === "Remove At" && cardList[Number(value)] !== undefined) {
  30.             let cardIndex = Number(value);
  31.             cardList.splice(cardIndex, 1);
  32.             console.log("Card successfully removed");
  33.         } else if (cmd === "Remove At") {
  34.             console.log("Index out of range");
  35.         }
  36.  
  37.         if (cmd === "Insert" && cardList[Number(value)] !== undefined && cardList[Number(value)] === cmdList[2]) {
  38.             console.log("Card is already in the deck");
  39.         } else if (cmd === "Insert" && cardList[Number(value)] !== undefined) {
  40.             let cardIndex = Number(value);
  41.             cardList.splice(cardIndex, 0, cmdList[2]);
  42.             console.log("Card successfully added");
  43.         } else if (cmd === "Insert") {
  44.             console.log("Index out of range");
  45.         }
  46.     }
  47.     console.log(cardList.join(`, `));
  48. }
Advertisement
Add Comment
Please, Sign In to add comment