Skip to content

Commit 608a309

Browse files
committed
Functions, assertions, and invalid ranks tested. Modified the if statement to handle valid numeric literals such as '2.1' and '002'.
1 parent 9abc5a9 commit 608a309

File tree

2 files changed

+77
-14
lines changed

2 files changed

+77
-14
lines changed

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

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,16 @@
99
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
1010
function getCardValue(card) {
1111

12-
const rank = card.slice(0,-1);
13-
if (rank === "A") {
14-
return 11;
15-
}
16-
else if (["K", "J", "Q", "10"].includes(rank)){
17-
return 10;
18-
}
19-
else if (!isNaN(rank )) {
20-
return Number(rank);
21-
}
22-
else {
23-
throw new Error ("Invalid Card");
24-
}
12+
const rank = card.slice(0, -1);
13+
const validRanks = ["A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2"];
14+
15+
if (!validRanks.includes(rank)) {
16+
throw new Error("Invalid Card");
17+
}
18+
19+
if (rank === "A") return 11;
20+
if (["K", "Q", "J", "10"].includes(rank)) return 10;
21+
return Number(rank);
2522
}
2623

2724
// The line below allows us to load the getCardValue function into tests in other files.
@@ -88,4 +85,20 @@ try {
8885
assertEquals(error.message, "Invalid Card");
8986
}
9087

91-
// Functions, assertion and invalid rank tested
88+
try {
89+
getCardValue("2.1♥"); // decimal — invalid
90+
console.log("Test failed: no error thrown for 2.1♥");
91+
} catch (error) {
92+
assertEquals(error.message, "Invalid Card");
93+
}
94+
95+
try {
96+
getCardValue("0002♦"); // padded number — invalid
97+
console.log("Test failed: no error thrown for 0002♦");
98+
} catch (error) {
99+
assertEquals(error.message, "Invalid Card");
100+
}
101+
102+
// Functions, assertion and invalid rank tested, and modified the if statment
103+
// to hadnle valid numeric literals such as "2.1", "002" "Functions, assertions, and invalid ranks tested.
104+
// Modified the if statement to handle valid numeric literals such as '2.1' and '002'.

Sprint-3/2-practice-tdd/count.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,53 @@ test("should count multiple occurrences of a character", () => {
2222
// And a character char that does not exist within the case-sensitive str,
2323
// When the function is called with these inputs,
2424
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
25+
26+
test("should return 0 when character does not exist in string", () => {
27+
const str = "Code Your Future";
28+
const char = "g";
29+
const count = countChar(str, char);
30+
expect(count).toEqual(0);
31+
});
32+
33+
// Scenario: Empty string
34+
35+
test("should return 0 when string is empty", () => {
36+
const str = "";
37+
const char = "a";
38+
const count = countChar(str, char);
39+
expect(count).toEqual(0);
40+
});
41+
42+
// Scenario: String is a space
43+
44+
test("should return 0 when string is space", () => {
45+
const str = " ";
46+
const char = "a";
47+
const count = countChar(str, char);
48+
expect(count).toEqual(0);
49+
});
50+
51+
// Scenario: String is
52+
53+
// Scenario: Empty character
54+
55+
test("should return 0 when character is empty", () => {
56+
const str = "my code";
57+
const char = "";
58+
const count = countChar(str, char);
59+
expect(count).toEqual(0);
60+
});
61+
62+
// Scenario: char is empty character
63+
64+
test("should return 0 when character is empty string ", () => {
65+
const str = "my code";
66+
const char = " ";
67+
const count = countChar(str, char);
68+
expect(count).toEqual(0);
69+
});
70+
71+
72+
73+
// Scenario: charcter not string
74+

0 commit comments

Comments
 (0)