Skip to content

Commit d8839f1

Browse files
committed
written function for get-card-value
1 parent 09cd676 commit d8839f1

1 file changed

Lines changed: 39 additions & 1 deletion

File tree

‎implement-and-rewrite-tests/implement/3-get-card-value.js‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,45 @@
2222
// Acceptance criteria:
2323
// After you have implemented the function, write tests to cover all the cases, and
2424
// execute the code to ensure all tests pass.
25+
//const rank = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
26+
//const suit = ["♠", "♥", "♦", "♣"];
27+
//console.log(suit[1]);
28+
//const card = `${number}${suit}`
29+
30+
//if the user input the rank only, the output is throwing an error.
31+
//expect the format is 2 digit and follow the suit.
32+
//validate input example: 3♠.
33+
//Required the user input the card must include a suit. extract the final character from the input.
2534

2635
export function getCardValue(card) {
27-
// TODO: Implement this function
36+
const rank = card.slice(0, -1);
37+
const suit = card.slice(-1);
38+
const validRank = [
39+
"A",
40+
"2",
41+
"3",
42+
"4",
43+
"5",
44+
"6",
45+
"7",
46+
"8",
47+
"9",
48+
"10",
49+
"J",
50+
"Q",
51+
"K",
52+
].includes(rank);
53+
const validSuit = ["♠", "♥", "♦", "♣"].includes(suit);
54+
55+
if (!validRank || !validSuit) {
56+
throw new Error (`Expected a number followed by a suit, but got "${card}"`);
57+
}
58+
59+
if (rank === "A") {
60+
return 11;
61+
} else if (["J", "Q", "K"].includes(rank)) {
62+
return 10;
63+
} else {
64+
return Number(rank);
65+
}
2866
}

0 commit comments

Comments
 (0)