Your cart is currently empty!
Dealing Cards, or “Chunking” Arrays
When reviewing code, I often find that although I think “understand” what’s happening… it’s often when I try to put myself “into the shoes of the interpreter/compiler” that I realize some flaw in my perception.
“Chunking Arrays” in JavaScript is one of those things. Though possible to force myself to memorize code using “totals” or “tempArray” or “currentVal”, real world “use cases” helps make the abstract a bit more concrete.
Below is code that I think illustrates what’s happening a bit better. In terms of Coding Interviews, where you might be asked to chunk an array, or perhaps you’re asked: “Build a card counting program for a magician to keep track of ‘some such thing’…” you can follow along the console.logs
to get a better sense of what’s happening as it does.
function dealCards(theCardsToBeDealt, theHandSize) {
const allTheHands = [];
for (let card of theCardsToBeDealt) {
const aHand = allTheHands[allTheHands.length - 1];
console.log("the hand as it is being dealt contains", aHand)
if (!aHand) {
allTheHands.push([card]);
console.log("All the hands are now ", allTheHands)
} else if (aHand.length === theHandSize) {
allTheHands.push([card]);
console.log("All the hands are now ", allTheHands)
} else {
aHand.push(card);
console.log("The hand now being dealt is ", aHand)
}
}
return console.log("ALL THE HANDS DEALT ARE: ", allTheHands);
}
Below is a an example of what running the following code will return:
dealCards(["Ace of Spades", "Two of Hearts", "King of Hearts", "Three of Clubs", "Four of Diamonds", "Seven of Clubs", "Nine of Diamonds", "Ten of Hearts", "Ace of Hearts"], 3)
//returns ...
the hand as it is being dealt contains undefined
All the hands are now [ [ 'Ace of Spades' ] ]
the hand as it is being dealt contains [ 'Ace of Spades' ]
The hand now being dealt is [ 'Ace of Spades', 'Two of Hearts' ]
the hand as it is being dealt contains [ 'Ace of Spades', 'Two of Hearts' ]
The hand now being dealt is [ 'Ace of Spades', 'Two of Hearts', 'King of Hearts' ]
the hand as it is being dealt contains [ 'Ace of Spades', 'Two of Hearts', 'King of Hearts' ]
All the hands are now [
[ 'Ace of Spades', 'Two of Hearts', 'King of Hearts' ],
[ 'Three of Clubs' ]
]
the hand as it is being dealt contains [ 'Three of Clubs' ]
The hand now being dealt is [ 'Three of Clubs', 'Four of Diamonds' ]
the hand as it is being dealt contains [ 'Three of Clubs', 'Four of Diamonds' ]
The hand now being dealt is [ 'Three of Clubs', 'Four of Diamonds', 'Seven of Clubs' ]
the hand as it is being dealt contains [ 'Three of Clubs', 'Four of Diamonds', 'Seven of Clubs' ]
All the hands are now [
[ 'Ace of Spades', 'Two of Hearts', 'King of Hearts' ],
[ 'Three of Clubs', 'Four of Diamonds', 'Seven of Clubs' ],
[ 'Nine of Diamonds' ]
]
the hand as it is being dealt contains [ 'Nine of Diamonds' ]
The hand now being dealt is [ 'Nine of Diamonds', 'Ten of Hearts' ]
the hand as it is being dealt contains [ 'Nine of Diamonds', 'Ten of Hearts' ]
The hand now being dealt is [ 'Nine of Diamonds', 'Ten of Hearts', 'Ace of Hearts' ]
ALL THE HANDS DEALT ARE: [
[ 'Ace of Spades', 'Two of Hearts', 'King of Hearts' ],
[ 'Three of Clubs', 'Four of Diamonds', 'Seven of Clubs' ],
[ 'Nine of Diamonds', 'Ten of Hearts', 'Ace of Hearts' ]
]