Skip to content
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
implemented card value logic and added tests for face cards and error…
… handling
  • Loading branch information
alexandru-pocovnicu committed Feb 18, 2026
commit 2e73bdc2898918db13ee74a02cfe034656a4f7b0
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,24 @@
// write one test at a time, and make it pass, build your solution up methodically
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
function getCardValue(card) {
let rank = card.slice(0, -1);
let cardFace = card[card.length - 1];

if (!["♠", "♥", "♦", "♣"].includes(cardFace)) {
throw new Error(`Invalid card face: ${cardFace}`);
}
if (rank === "A") {
return 11;
}
if (+rank >= 2 && +rank <= 9) {
return +rank;
}
if (["K", "10", "Q", "J"].includes(rank)) {
return 10;
}
if (!["♠", "♥", "♦", "♣"].includes(cardFace)) {
}
throw new Error(`Invalid card rank: ${rank}`);
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -26,26 +41,28 @@ function assertEquals(actualOutput, targetOutput) {
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

// Acceptance criteria:

// Given a card string in the format "A♠" (representing a card in blackjack - the last character will always be an emoji for a suit, and all characters before will be a number 2-10, or one letter of J, Q, K, A),
// When the function getCardValue is called with this card string as input,
// Then it should return the numerical card value
const aceofSpades = getCardValue("A♠");
assertEquals(aceofSpades, 11);
const aceOfSpades = getCardValue("A♠");
assertEquals(aceOfSpades, 11);

// Handle Number Cards (2-10):
// Given a card with a rank between "2" and "9",
// When the function is called with such a card,
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
const fiveofHearts = getCardValue("5♥");
// ====> write your test here, and then add a line to pass the test in the function above
const fiveOfHearts = getCardValue("5♥");
assertEquals(fiveOfHearts, 5);

// Handle Face Cards (J, Q, K):
// Given a card with a rank of "10," "J," "Q," or "K",
// When the function is called with such a card,
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.

const kingOfHearts = getCardValue("K♥");
assertEquals(kingOfHearts, 10);
// Handle Ace (A):
// Given a card with a rank of "A",
// When the function is called with an Ace,
Expand All @@ -55,3 +72,16 @@ const fiveofHearts = getCardValue("5♥");
// Given a card with an invalid rank (neither a number nor a recognized face card),
// When the function is called with such a card,
// Then it should throw an error indicating "Invalid card rank."
try {
getCardValue("2");
throw new Error("Should have thrown an error for invalid card");
} catch (error) {
assertEquals(error.message, "Invalid card face: 2");
}

try {
getCardValue("1♥");
throw new Error("Should have thrown an error for invalid card");
} catch (error) {
assertEquals(error.message, "Invalid card rank: 1");
}