vladovip

DeckOfCards

Feb 20th, 2022
783
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function deckOfCards (array){
  2.  
  3.     let deck = array.shift().split(", ")
  4.     let number = Number(array.shift())// с Number отпред явно работи
  5.      
  6.     for (let i = 0; i < number; i++){
  7.         let tokens = array[i].split(", ")
  8.         let command = tokens[0]
  9.         let indexName = tokens[1]
  10.         let cardName = tokens[2]
  11.      
  12.         if(command === "Add"){
  13.             //let index = Number(indexName) ще го ползвам, ако не работи с includes, но не мисля, че ще се наложи
  14.             if(deck.includes(indexName)){// да видя дали ще има смисъл с deck[index] === indexName
  15.                 console.log("Card is already in the deck")
  16.             }else{
  17.                 deck.push(indexName)
  18.                 console.log("Card successfully added")
  19.             }
  20.      
  21.         }
  22.      
  23.         else if(command === "Remove"){
  24.      
  25.             if(deck.includes(indexName)){
  26.                 let findIndex = deck.indexOf(indexName)
  27.                 deck.splice(findIndex, 1)
  28.                 console.log("Card successfully removed")
  29.             }else{
  30.                 console.log("Card not found")
  31.             }
  32.      
  33.         }
  34.      
  35.         else if(command === "Remove At"){
  36.             let currentIndex = Number(indexName)
  37.             if(currentIndex >= 0 && currentIndex < deck.length){
  38.                 deck.splice(currentIndex, 1)
  39.                 console.log("Card successfully removed")
  40.      
  41.             }else{
  42.                 console.log("Index out of range")
  43.             }
  44.      
  45.         }
  46.      
  47.         else if(command === "Insert"){
  48.             let currentIndex = Number(indexName)
  49.             if(currentIndex >= 0 && currentIndex < deck.length){
  50.                 if(deck.includes(cardName)){
  51.                     console.log("Card is already added")
  52.                 }else{
  53.                     deck.splice(currentIndex, 0, cardName)
  54.                     console.log("Card successfully added")
  55.                 }
  56.      
  57.             }else{
  58.                 console.log("Index out of range")
  59.             }
  60.      
  61.         }
  62.      
  63.     }
  64.      
  65.     console.log(deck.join(", "))
  66.        
  67.     }
  68.  
  69.    
  70.     deckOfCards(["Jack of Spades, Ace of Clubs, Jack of Clubs",
  71.     "2",
  72.     "Insert, -1, Queen of Spades",
  73.     "Remove At, 1"])
Advertisement
Add Comment
Please, Sign In to add comment