Skip to content

Commit 6794148

Browse files
author
russom
committed
If statements created inside the function to check for valid string of card rank and suits.
1 parent e05bc8f commit 6794148

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,41 @@
2323

2424
function getCardValue(card) {
2525
// TODO: Implement this function
26+
27+
const validRanks = [ // An array of ranks are created here
28+
"A",
29+
"2",
30+
"3",
31+
"4",
32+
"5",
33+
"6",
34+
"7",
35+
"8",
36+
"9",
37+
"10",
38+
"J",
39+
"Q",
40+
"K",
41+
];
42+
const validSuits = ["♠", "♥", "♦", "♣"]; // An array of suits are created here.
43+
44+
if (typeof card !== "string") { // Here the type of values are checked whether they are string.
45+
throw new Error("Invalid card: " + card);
46+
}
47+
const suit = card.slice(-1);
48+
const rank = card.slice(0, -1);
49+
50+
if (!validRanks.includes(rank) || !validSuits.includes(suit)) { //Here the values that are stored in rank and suit are checked whether they are valid are inside the validRank and validSuits arrays.
51+
throw new Error("Invalid card: " + card);
52+
}
53+
54+
if (rank === "A") { // Here the code is testing wether the rank is an Ace.
55+
return 11;
56+
} else if (cards === "J" || rank === "Q" || rank === "K") { // Here the ranks are checked if they are J, Q or K and it returns 10
57+
return 10;
58+
} else { // Lastly any rank between 2 and 10 get checked and the same number of value returned.
59+
return Number(rank);
60+
}
2661
}
2762

2863
// The line below allows us to load the getCardValue function into tests in other files.

0 commit comments

Comments
 (0)