Skip to content

Commit 241ef30

Browse files
committed
implemented function and tested
1 parent b28e30e commit 241ef30

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,16 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
let removeSuit = card.slice(0, -1);
26+
if (removeSuit === "A") {
27+
return 11;
28+
} else if (removeSuit === "J" || removeSuit === "Q" ||removeSuit === "K") {
29+
return 10;
30+
} else if (removeSuit > 1 && removeSuit < 11) {
31+
return Number(removeSuit);
32+
} else {
33+
throw new Error("Error")
34+
}
2635
}
2736

2837
// The line below allows us to load the getCardValue function into tests in other files.
@@ -40,15 +49,27 @@ function assertEquals(actualOutput, targetOutput) {
4049
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4150
// Examples:
4251
assertEquals(getCardValue("9♠"), 9);
52+
assertEquals(getCardValue("A♠"), 11);
53+
assertEquals(getCardValue("J♦"), 10);
54+
assertEquals(getCardValue("7♥"), 7);
55+
assertEquals(getCardValue("A♥"), 11);
56+
assertEquals(getCardValue("8♥"), 8);
4357

4458
// Handling invalid cards
4559
try {
4660
getCardValue("invalid");
47-
4861
// This line will not be reached if an error is thrown as expected
4962
console.error("Error was not thrown for invalid card 😢");
5063
} catch (e) {
5164
console.log("Error thrown for invalid card 🎉");
5265
}
5366

5467
// What other invalid card cases can you think of?
68+
try {
69+
getCardValue("15♥");
70+
// This line will not be reached if an error is thrown as expected
71+
console.error("Error was not thrown for invalid card 😢");
72+
} catch (e) {
73+
console.log("Error thrown for invalid card 🎉");
74+
}
75+

0 commit comments

Comments
 (0)