Skip to content

Commit 1ec84d6

Browse files
Finished last part 1 the first section of Sprint-3 /1-implement-and-rewrite-tests.
1 parent 0359cea commit 1ec84d6

File tree

4 files changed

+125
-1
lines changed

4 files changed

+125
-1
lines changed

Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313
function isProperFraction(numerator, denominator) {
1414
// TODO: Implement this function
15+
if ( numerator > 0 && denominator > 0 && denominator !==0 && numerator < denominator){
16+
return true;
17+
} else {
18+
return false
19+
}
1520
}
1621

1722
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -31,3 +36,9 @@ function assertEquals(actualOutput, targetOutput) {
3136

3237
// Example: 1/2 is a proper fraction
3338
assertEquals(isProperFraction(1, 2), true);
39+
assertEquals(isProperFraction("2", 2), false);
40+
assertEquals(isProperFraction(3, 4), true);
41+
assertEquals(isProperFraction(5, 10), true);
42+
assertEquals(isProperFraction(- 4, 2), false);
43+
assertEquals(isProperFraction(- 4, 10), false);
44+
assertEquals(isProperFraction(123123213n, 0), false);

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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,44 @@
2323

2424
function getCardValue(card) {
2525
// TODO: Implement this function
26+
const cardRanks = [
27+
"A",
28+
"2",
29+
"3",
30+
"4",
31+
"5",
32+
"6",
33+
"7",
34+
"8",
35+
"9",
36+
"10",
37+
"J",
38+
"Q",
39+
"K",
40+
];
41+
const cardSuits = ["♠", "♥", "♦", "♣"];
42+
if (
43+
typeof card !== "string" ||
44+
!cardRanks.includes(card.slice(0,-1)) ||
45+
!cardSuits.includes(card.slice(-1))
46+
) {
47+
throw new Error("Invalid card");
48+
}
49+
50+
switch (card.slice(0, -1)) {
51+
case "A":
52+
return 11;
53+
case "J":
54+
case "Q":
55+
case "K":
56+
return 10;
57+
default:
58+
return Number(card.slice(0, -1));
59+
}
2660
}
2761

62+
63+
2864
// The line below allows us to load the getCardValue function into tests in other files.
2965
// This will be useful in the "rewrite tests with jest" step.
3066
module.exports = getCardValue;
@@ -40,13 +76,28 @@ function assertEquals(actualOutput, targetOutput) {
4076
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4177
// Examples:
4278
assertEquals(getCardValue("9♠"), 9);
79+
assertEquals(getCardValue("A♥"), 11);
80+
assertEquals(getCardValue("J♥"), 10);
81+
assertEquals(getCardValue("Q♦"), 10);
82+
assertEquals(getCardValue("K♣"), 10);
83+
assertEquals(getCardValue("10♠"), 10);
84+
assertEquals(getCardValue("5♠"), 5);
4385

4486
// Handling invalid cards
4587
try {
4688
getCardValue("invalid");
47-
4889
// This line will not be reached if an error is thrown as expected
4990
console.error("Error was not thrown for invalid card");
5091
} catch (e) {}
5192

93+
try {
94+
getCardValue("A");
95+
console.error("Expected error for 'A' but none was thrown");
96+
} catch (e) {}
97+
98+
try {
99+
getCardValue("♠");
100+
console.error("Expected error for '♠' but none was thrown");
101+
} catch (e) {}
102+
52103
// What other invalid card cases can you think of?

Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,21 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
88
test(`should return false when denominator is zero`, () => {
99
expect(isProperFraction(1, 0)).toEqual(false);
1010
});
11+
test(`should return false when numerator is zero`, () => {
12+
expect(isProperFraction(0, 1)).toEqual(false);
13+
});
14+
test(`should return false when numerator is negative`, () => {
15+
expect(isProperFraction(-1, 2)).toEqual(false);
16+
});
17+
test(`should return false when denominator is negative`, () => {
18+
expect(isProperFraction(1, -2)).toEqual(false);
19+
});
20+
test(`should return false when denominator is infinity`, () => {
21+
expect(isProperFraction(1, 23443243n)).toEqual(true);
22+
});
23+
test(`should return false when numerator is infinity`, () => {
24+
expect(isProperFraction(23432434n, 10)).toEqual(false);
25+
});
26+
test(`should return true when denominator is bigger then numerator`, () => {
27+
expect(isProperFraction(5, 10)).toEqual(true);
28+
});

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,52 @@ test(`Should return 11 when given an ace card`, () => {
1111

1212
// Suggestion: Group the remaining test data into these categories:
1313
// Number Cards (2-10)
14+
test("Should return 2 for '2♠'", () => {
15+
expect(getCardValue("2♠")).toEqual(2);
16+
});
17+
18+
test("Should return 5 for '5♥'", () => {
19+
expect(getCardValue("5♥")).toEqual(5);
20+
});
21+
22+
test("Should return 9 for '9♦'", () => {
23+
expect(getCardValue("9♦")).toEqual(9);
24+
});
25+
26+
test("Should return 10 for '10♣'", () => {
27+
expect(getCardValue("10♣")).toEqual(10);
28+
});
29+
1430
// Face Cards (J, Q, K)
31+
test("Should return 10 for 'J♠'", () => {
32+
expect(getCardValue("J♠")).toEqual(10);
33+
});
34+
35+
test("Should return 10 for 'Q♥'", () => {
36+
expect(getCardValue("Q♥")).toEqual(10);
37+
});
38+
39+
test("Should return 10 for 'K♦'", () => {
40+
expect(getCardValue("K♦")).toEqual(10);
41+
});
42+
1543
// Invalid Cards
44+
test("Should throw error for missing suit (e.g., 'A')", () => {
45+
expect(() => getCardValue("A")).toThrow("Invalid card");
46+
});
47+
48+
test("Should throw error for missing rank (e.g., '♠')", () => {
49+
expect(() => getCardValue("♠")).toThrow("Invalid card");
50+
});
51+
52+
test("Should throw error for null", () => {
53+
expect(() => getCardValue(null)).toThrow("Invalid card");
54+
});
55+
56+
test("Should throw error for undefined", () => {
57+
expect(() => getCardValue(undefined)).toThrow("Invalid card");
58+
});
59+
1660

1761
// To learn how to test whether a function throws an error as expected in Jest,
1862
// please refer to the Jest documentation:

0 commit comments

Comments
 (0)