Skip to content

Commit 2a7471c

Browse files
committed
practice tdd task is done
1 parent e3b6258 commit 2a7471c

File tree

5 files changed

+69
-8
lines changed

5 files changed

+69
-8
lines changed

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,36 @@
22
// We will use the same function, but write tests for it using Jest in this file.
33
const getCardValue = require("../implement/3-get-card-value");
44

5+
// Case 1: Handle Ace
56
test("should return 11 for Ace of Spades", () => {
67
const aceofSpades = getCardValue("A♠");
78
expect(aceofSpades).toEqual(11);
89
});
910

10-
// Case 2: Handle Number Cards (2-10):
11-
// Case 3: Handle Face Cards (J, Q, K):
12-
// Case 4: Handle Ace (A):
13-
// Case 5: Handle Invalid Cards:
11+
// Case 2: Handle Number Cards (2-10)
12+
// When the card is "2" to "10", it should return the numeric value
13+
test("should return the correct value for number cards", () => {
14+
expect(getCardValue("5♥")).toEqual(5);
15+
expect(getCardValue("2♦")).toEqual(2);
16+
expect(getCardValue("10♣")).toEqual(10);
17+
});
18+
19+
// Case 3: Handle Face Cards (J, Q, K)
20+
// When the card is "J", "Q", or "K", it should return 10
21+
test("should return 10 for face cards J, Q, K", () => {
22+
expect(getCardValue("J♣")).toEqual(10);
23+
expect(getCardValue("Q♥")).toEqual(10);
24+
expect(getCardValue("K♠")).toEqual(10);
25+
});
26+
27+
// Case 4: Handle Ace (already handled in Case 1, but can check another suit)
28+
test("should return 11 for Ace of Hearts", () => {
29+
expect(getCardValue("A♥")).toEqual(11);
30+
});
31+
32+
// Case 5: Handle Invalid Cards
33+
// If the card rank is invalid, it should throw an error
34+
test("should throw error for invalid cards", () => {
35+
expect(() => getCardValue("X♠")).toThrow("Invalid card rank.");
36+
expect(() => getCardValue("11♣")).toThrow("Invalid card rank.");
37+
});

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
let count = 0;
3+
for (let i = 0; i < stringOfCharacters.length; i++) {
4+
if (stringOfCharacters[i] === findCharacter) {
5+
count++;
6+
}
7+
}
8+
return count;
39
}
410

511
module.exports = countChar;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,10 @@ 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 the character does not exist in the string", () => {
27+
const str = "hello";
28+
const char = "z";
29+
const count = countChar(str, char);
30+
expect(count).toEqual(0);
31+
});
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
function getOrdinalNumber(num) {
2-
return "1st";
1+
function getOrdinalNumber(number) {
2+
if (number === 1) return "1st";
3+
if (number === 2) return "2nd";
4+
if (number === 3) return "3rd";
5+
return number + "th";
36
}
47

58
module.exports = getOrdinalNumber;

Sprint-3/2-practice-tdd/get-ordinal-number.test.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// get-ordinal-number.test.js
12
const getOrdinalNumber = require("./get-ordinal-number");
23
// In this week's prep, we started implementing getOrdinalNumber
34

@@ -7,7 +8,27 @@ const getOrdinalNumber = require("./get-ordinal-number");
78
// Case 1: Identify the ordinal number for 1
89
// When the number is 1,
910
// Then the function should return "1st"
10-
1111
test("should return '1st' for 1", () => {
1212
expect(getOrdinalNumber(1)).toEqual("1st");
1313
});
14+
15+
// Case 2: Identify the ordinal number for 2
16+
// When the number is 2,
17+
// Then the function should return "2nd"
18+
test("should return '2nd' for 2", () => {
19+
expect(getOrdinalNumber(2)).toEqual("2nd");
20+
});
21+
22+
// Case 3: Identify the ordinal number for 3
23+
// When the number is 3,
24+
// Then the function should return "3rd"
25+
test("should return '3rd' for 3", () => {
26+
expect(getOrdinalNumber(3)).toEqual("3rd");
27+
});
28+
29+
// Case 4: Identify the ordinal number for 4
30+
// When the number is 4,
31+
// Then the function should return "4th"
32+
test("should return '4th' for 4", () => {
33+
expect(getOrdinalNumber(4)).toEqual("4th");
34+
});

0 commit comments

Comments
 (0)