Algorithm for counting number of decks in a given pack of cards

4.11K viewsProgrammingalgorithms cards deck javascript

You are given a pack of cards like
(["9C", "KS", "AC", "AH", "8D", "4C", "KD", "JC", "7D", "9D", "2H", "7C", "3C", "7S", "5C", "6H", "TH"]));
Where C, S, D, H stand for clubs, spade, Diamond and hearts respectively.
A given deck of cards has cards with values 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A
where T = 10, J = jack, Q = queen, K = King and A = ace
Write a function deckOfCards() which will accept and array of cards as given above
and returns the number of decks present in that card array.

Answered question
this.decksOfCards(["2S", "2C", "2D", "2H", "3S", "3C", "3D", "3H", "4S", "4C", 
"4D", "4H", "5S", "5C", "5D", "5H", "6S", "6C", "6D", "6H", "7S", "7C", "7D", "7H", 
"8S", "8C", "8D", "8H", "9S", "9C", "9D", "9H", "TS", "TC", "TD", "TH", "JS", "JC", 
"JD", "JH", "QS", "QC", "QD", "QH", "KS", "KC", "KD", "KH", "AS", "AC", "AD", "AH", 
"2S", "2C", "2D", "2H", "3S", "3C", "3D", "3H", "4S", "4C", "4D", "4H", "5S", "5C", 
"5D", "5H", "6S", "6C", "6D", "6H", "7S", "7C", "7D", "7H", "8S", "8C", "8D", "8H", 
"9S", "9C", "9D", "9H", "TS", "TC", "TD", "TH", "JS", "JC", "JD", "JH", "QS", "QC", 
"QD", "QH", "KS", "KC", "KD", "KH", "AS", "AC", "AD", "AH", "2S", "2C", "2D", "2H", 
"3S", "3C", "3D", "3H", "4S", "4C", "4D", "4H", "5S", "5C", "5D", "5H", "6S", "6C", 
"6D", "6H", "7S", "7C", "7D", "7H", "8S", "8C", "8D", "8H", "9S", "9C", "9D", "9H", 
"TS", "TC", "TD", "TH", "JS", "JC", "JD", "JH", "QS", "QC", "QD", "QH", "KS", "KC", 
"KD", "KH", "AS", "AC", "AD"]);//output 2
decksOfCards = (inputCardArr) => {
  let faces = [ "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A" ];
  let suites = [ "S", "C", "H", "D" ];
  let acceptedCards = [];
     for (var f = 0; f<faces.length; f++) {
    for (var s = 0; s<suites.length; s++) {
      acceptedCards.push(faces[f] + suites[s]);
     let decks = [];
     for (var i = 0; i < inputCardArr.length; i++) {
    if (!acceptedCards.includes(inputCardArr[i])) {
      //check invalid card added by user
         let added = false;
         for (var j = 0; j < decks.length; j++){
      //decks cards arr check value
      if (!decks[j].cards.includes(inputCardArr[i])){
        added = true;
     if (!added){
  const fullDeckCount = decks.filter(d => == 52).length;


  • “decks=[{\”cards\”:[\”2S\”,\”2C\”,\”2D\”,\”2H\”,\”3S\”,\”3C\”,\”3D\”,\”3H\”,\”4S\”,\”4C\”,\”4D\”,\”4H\”,\”5S\”,\”5C\”,\”5D\”,\”5H\”,\”6S\”,\”6C\”,\”6D\”,\”6H\”,\”7S\”,\”7C\”,\”7D\”,\”7H\”,\”8S\”,\”8C\”,\”8D\”,\”8H\”,\”9S\”,\”9C\”,\”9D\”,\”9H\”,\”TS\”,\”TC\”,\”TD\”,\”TH\”,\”JS\”,\”JC\”,\”JD\”,\”JH\”,\”QS\”,\”QC\”,\”QD\”,\”QH\”,\”KS\”,\”KC\”,\”KD\”,\”KH\”,\”AS\”,\”AC\”,\”AD\”,\”AH\”]},{\”cards\”:[\”2S\”,\”2C\”,\”2D\”,\”2H\”,\”3S\”,\”3C\”,\”3D\”,\”3H\”,\”4S\”,\”4C\”,\”4D\”,\”4H\”,\”5S\”,\”5C\”,\”5D\”,\”5H\”,\”6S\”,\”6C\”,\”6D\”,\”6H\”,\”7S\”,\”7C\”,\”7D\”,\”7H\”,\”8S\”,\”8C\”,\”8D\”,\”8H\”,\”9S\”,\”9C\”,\”9D\”,\”9H\”,\”TS\”,\”TC\”,\”TD\”,\”TH\”,\”JS\”,\”JC\”,\”JD\”,\”JH\”,\”QS\”,\”QC\”,\”QD\”,\”QH\”,\”KS\”,\”KC\”,\”KD\”,\”KH\”,\”AS\”,\”AC\”,\”AD\”,\”AH\”]},{\”cards\”:[\”2S\”,\”2C\”,\”2D\”,\”2H\”,\”3S\”,\”3C\”,\”3D\”,\”3H\”,\”4S\”,\”4C\”,\”4D\”,\”4H\”,\”5S\”,\”5C\”,\”5D\”,\”5H\”,\”6S\”,\”6C\”,\”6D\”,\”6H\”,\”7S\”,\”7C\”,\”7D\”,\”7H\”,\”8S\”,\”8C\”,\”8D\”,\”8H\”,\”9S\”,\”9C\”,\”9D\”,\”9H\”,\”TS\”,\”TC\”,\”TD\”,\”TH\”,\”JS\”,\”JC\”,\”JD\”,\”JH\”,\”QS\”,\”QC\”,\”QD\”,\”QH\”,\”KS\”,\”KC\”,\”KD\”,\”KH\”,\”AS\”,\”AC\”,\”AD\”]}]”
  • output = 2
Changed status to publish
You are viewing 1 out of 3 answers, click here to view all answers.
Write your answer.