Skip to content

Commit a2adf3d

Browse files
author
Payman IB
committed
Implement getCardValue function to return numerical values for playing cards and handle invalid ranks
1 parent 90a9885 commit a2adf3d

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,21 @@
88
// write one test at a time, and make it pass, build your solution up methodically
99
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
1010
function getCardValue(card) {
11+
const rank = card.slice(0, -1);
12+
1113
if (rank === "A") {
1214
return 11;
1315
}
16+
else if ( 2 <= rank && rank <= 9 ){
17+
return Number(rank);
18+
}
19+
else if (rank === "10" || rank === "J" || rank === "Q" || rank === "K") {
20+
return 10;
21+
}
22+
else {
23+
throw new Error("Invalid card rank.");
24+
}
1425
}
15-
1626
// The line below allows us to load the getCardValue function into tests in other files.
1727
// This will be useful in the "rewrite tests with jest" step.
1828
module.exports = getCardValue;

0 commit comments

Comments
 (0)