diff --git a/deck.js b/deck.js new file mode 100644 index 00000000..32886f81 --- /dev/null +++ b/deck.js @@ -0,0 +1,52 @@ +var makeDeck = function () { + // Initialise an empty deck array + var cardDeck = []; + // Initialise an array of the 4 suits in our deck. We will loop over this array. + var suits = ['hearts', 'diamonds', 'clubs', 'spades']; + + // Loop over the suits array + var suitIndex = 0; + while (suitIndex < suits.length) { + // Store the current suit in a variable + var currentSuit = suits[suitIndex]; + + // Loop from 1 to 13 to create all cards for a given suit + // Notice rankCounter starts at 1 and not 0, and ends at 13 and not 12. + // This is an example of a loop without an array. + var rankCounter = 1; + while (rankCounter <= 13) { + // By default, the card name is the same as rankCounter + var cardName = rankCounter; + + // If rank is 1, 11, 12, or 13, set cardName to the ace or face card's name + if (cardName == 1) { + cardName = 'ace'; + } else if (cardName == 11) { + cardName = 'jack'; + } else if (cardName == 12) { + cardName = 'queen'; + } else if (cardName == 13) { + cardName = 'king'; + } + + // Create a new card with the current name, suit, and rank + var card = { + name: cardName, + suit: currentSuit, + rank: rankCounter, + }; + + // Add the new card to the deck + cardDeck.push(card); + + // Increment rankCounter to iterate over the next rank + rankCounter += 1; + } + + // Increment the suit index to iterate over the next suit + suitIndex += 1; + } + + // Return the completed card deck + return cardDeck; +}; \ No newline at end of file diff --git a/index.html b/index.html index bbc7dffd..d05eaf55 100644 --- a/index.html +++ b/index.html @@ -129,7 +129,9 @@

♣️ Basics - Blackjack ♠️

Input:


- + + +

Output:

@@ -137,11 +139,39 @@

♣️ Basics - Blackjack ♠️