Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions deck.js
Original file line number Diff line number Diff line change
@@ -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;
};
36 changes: 33 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,49 @@ <h1 class="header">♣️ Basics - Blackjack ♠️</h1>
<p>Input:</p>
<input id="input-field" />
<br />
<button id="submit-button">Submit</button>
<button id="play-button">Play</button>
<button id="hit-button">Hit</button>
<button id="stand-button">Stand</button>
<p>Output:</p>
<div id="output-div"></div>
</div>
<!-- Import program logic -->
<script src="script.js"></script>
<!-- Define button click functionality -->
<script>
var button = document.querySelector("#submit-button");
var button = document.querySelector("#play-button");
button.addEventListener("click", function () {
// Set result to input value
var input = document.querySelector("#input-field");
var result = main(input.value);
var result = getResult(input.value);

// Display result in output element
var output = document.querySelector("#output-div");
output.innerHTML = result;

// Reset input value
input.value = "";
});
//
//
var button = document.querySelector("#hit-button");
button.addEventListener("click", function () {
// Set result to input value
var input = document.querySelector("#input-field");
var result = hit(input.value);

// Display result in output element
var output = document.querySelector("#output-div");
output.innerHTML = result;

// Reset input value
input.value = "";
});
var button = document.querySelector("#stand-button");
button.addEventListener("click", function () {
// Set result to input value
var input = document.querySelector("#input-field");
var result = stand(input.value);

// Display result in output element
var output = document.querySelector("#output-div");
Expand Down
Loading