Skip to content

Commit 3934b5d

Browse files
committed
Added Jest test files for angle type, proper fraction, and card value functions
1 parent 8f3d6cf commit 3934b5d

File tree

6 files changed

+116
-7
lines changed

6 files changed

+116
-7
lines changed

Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010
function getAngleType(angle) {
1111
if (angle === 90) {
1212
return "Right angle";
13+
} else if (angle < 90 && angle > 0) {
14+
return "Acute angle";
15+
} else if (angle > 90 && angle < 180) {
16+
return "Obtuse angle";
17+
} else if (angle === 180) {
18+
return "Straight angle";
19+
} else if (angle > 180 && angle < 360) {
20+
return "Reflex angle";
21+
} else {
22+
return "Invalid angle";
1323
}
1424
// Run the tests, work out what Case 2 is testing, and implement the required code here.
1525
// Then keep going for the other cases, one at a time.
@@ -50,14 +60,16 @@ assertEquals(acute, "Acute angle");
5060
// When the angle is greater than 90 degrees and less than 180 degrees,
5161
// Then the function should return "Obtuse angle"
5262
const obtuse = getAngleType(120);
53-
// ====> write your test here, and then add a line to pass the test in the function above
63+
assertEquals(obtuse, "Obtuse angle");
5464

5565
// Case 4: Identify Straight Angles:
5666
// When the angle is exactly 180 degrees,
5767
// Then the function should return "Straight angle"
58-
// ====> write your test here, and then add a line to pass the test in the function above
68+
const straight = getAngleType(180);
69+
assertEquals(straight, "Straight angle");
5970

6071
// Case 5: Identify Reflex Angles:
6172
// When the angle is greater than 180 degrees and less than 360 degrees,
6273
// Then the function should return "Reflex angle"
63-
// ====> write your test here, and then add a line to pass the test in the function above
74+
const reflex = getAngleType(270);
75+
assertEquals(reflex, "Reflex angle");

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@
88
// write one test at a time, and make it pass, build your solution up methodically
99

1010
function isProperFraction(numerator, denominator) {
11-
if (numerator < denominator) {
11+
// Handle invalid denominators (e.g., divide by zero)
12+
if (denominator === 0) {
13+
return false;
14+
}
15+
16+
// Compare absolute values for proper fraction check
17+
if (Math.abs(numerator) < Math.abs(denominator)) {
1218
return true;
19+
} else {
20+
return false;
1321
}
1422
}
1523

@@ -46,14 +54,20 @@ assertEquals(improperFraction, false);
4654
// target output: true
4755
// Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true.
4856
const negativeFraction = isProperFraction(-4, 7);
49-
// ====> complete with your assertion
57+
assertEquals(negativeFraction, true);
5058

5159
// Equal Numerator and Denominator check:
5260
// Input: numerator = 3, denominator = 3
5361
// target output: false
5462
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false.
5563
const equalFraction = isProperFraction(3, 3);
56-
// ====> complete with your assertion
64+
assertEquals(equalFraction, false);
5765

5866
// Stretch:
5967
// What other scenarios could you test for?
68+
// Zero Numerator check:
69+
// Input: numerator = 0, denominator = 5
70+
// target output: true
71+
// Explanation: The fraction 0/5 is a proper fraction because the numerator (0) is less than the denominator (5). The function should return true.
72+
const zeroNumerator = isProperFraction(0, 5);
73+
assertEquals(zeroNumerator, true);

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,26 @@
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+
// Extract rank (everything except the last character, which is the suit)
12+
const rank = card.slice(0, -1);
13+
14+
// Handle Ace
1115
if (rank === "A") {
1216
return 11;
1317
}
18+
19+
// Handle Face Cards
20+
if (["K", "Q", "J", "10"].includes(rank)) {
21+
return 10;
22+
}
23+
24+
// Handle Number Cards (2–9)
25+
if (!isNaN(rank) && Number(rank) >= 2 && Number(rank) <= 9) {
26+
return Number(rank);
27+
}
28+
29+
// Handle invalid inputs
30+
throw new Error("Invalid card rank");
1431
}
1532

1633
// The line below allows us to load the getCardValue function into tests in other files.
@@ -39,19 +56,38 @@ assertEquals(aceofSpades, 11);
3956
// When the function is called with such a card,
4057
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4158
const fiveofHearts = getCardValue("5♥");
42-
// ====> write your test here, and then add a line to pass the test in the function above
59+
assertEquals(fiveofHearts, 5);
4360

4461
// Handle Face Cards (J, Q, K):
4562
// Given a card with a rank of "10," "J," "Q," or "K",
4663
// When the function is called with such a card,
4764
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
65+
const jackOfDiamonds = getCardValue("J♦");
66+
assertEquals(jackOfDiamonds, 10);
67+
68+
const queenOfClubs = getCardValue("Q♣");
69+
assertEquals(queenOfClubs, 10);
70+
71+
const kingOfSpades = getCardValue("K♠");
72+
assertEquals(kingOfSpades, 10);
73+
74+
const tenOfHearts = getCardValue("10♥");
75+
assertEquals(tenOfHearts, 10);
76+
4877

4978
// Handle Ace (A):
5079
// Given a card with a rank of "A",
5180
// When the function is called with an Ace,
5281
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
82+
const aceofClubs = getCardValue("A♣");
83+
assertEquals(aceofClubs, 11);
5384

5485
// Handle Invalid Cards:
5586
// Given a card with an invalid rank (neither a number nor a recognized face card),
5687
// When the function is called with such a card,
5788
// Then it should throw an error indicating "Invalid card rank."
89+
try {
90+
getCardValue("X♣");
91+
} catch (error) {
92+
assertEquals(error.message, "Invalid card rank");
93+
}

Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,27 @@ test("should identify right angle (90°)", () => {
1212
// Case 2: Identify Acute Angles:
1313
// When the angle is less than 90 degrees,
1414
// Then the function should return "Acute angle"
15+
test("should identify acute angle (<90°)", () => {
16+
expect(getAngleType(45)).toEqual("Acute angle");
17+
});
1518

1619
// Case 3: Identify Obtuse Angles:
1720
// When the angle is greater than 90 degrees and less than 180 degrees,
1821
// Then the function should return "Obtuse angle"
22+
test("should identify obtuse angle (>90° and <180°)", () => {
23+
expect(getAngleType(120)).toEqual("Obtuse angle");
24+
});
1925

2026
// Case 4: Identify Straight Angles:
2127
// When the angle is exactly 180 degrees,
2228
// Then the function should return "Straight angle"
29+
test("should identify straight angle (180°)", () => {
30+
expect(getAngleType(180)).toEqual("Straight angle");
31+
});
2332

2433
// Case 5: Identify Reflex Angles:
2534
// When the angle is greater than 180 degrees and less than 360 degrees,
2635
// Then the function should return "Reflex angle"
36+
test("should identify reflex angle (>180° and <360°)", () => {
37+
expect(getAngleType(270)).toEqual("Reflex angle");
38+
});

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,18 @@ test("should return true for a proper fraction", () => {
77
});
88

99
// Case 2: Identify Improper Fractions:
10+
test("should return false for an improper fraction (numerator > denominator)", () => {
11+
expect(isProperFraction(5, 3)).toEqual(false);
12+
});
1013

1114
// Case 3: Identify Negative Fractions:
15+
test("should correctly handle negative fractions", () => {
16+
expect(isProperFraction(-1, 3)).toEqual(true);
17+
expect(isProperFraction(1, -3)).toEqual(true);
18+
expect(isProperFraction(-4, 3)).toEqual(false);
19+
});
1220

1321
// Case 4: Identify Equal Numerator and Denominator:
22+
test("should return false when numerator and denominator are equal", () => {
23+
expect(isProperFraction(3, 3)).toEqual(false);
24+
});

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,30 @@ test("should return 11 for Ace of Spades", () => {
88
});
99

1010
// Case 2: Handle Number Cards (2-10):
11+
test("should return correct value for number cards", () => {
12+
expect(getCardValue("2♥")).toEqual(2);
13+
expect(getCardValue("5♦")).toEqual(5);
14+
expect(getCardValue("9♣")).toEqual(9);
15+
expect(getCardValue("10♠")).toEqual(10);
16+
});
17+
1118
// Case 3: Handle Face Cards (J, Q, K):
19+
test("should return 10 for face cards (J, Q, K)", () => {
20+
expect(getCardValue("J♦")).toEqual(10);
21+
expect(getCardValue("Q♥")).toEqual(10);
22+
expect(getCardValue("K♣")).toEqual(10);
23+
});
24+
1225
// Case 4: Handle Ace (A):
26+
test("should return 11 for Ace of any suit", () => {
27+
expect(getCardValue("A♣")).toEqual(11);
28+
expect(getCardValue("A♦")).toEqual(11);
29+
expect(getCardValue("A♥")).toEqual(11);
30+
});
31+
1332
// Case 5: Handle Invalid Cards:
33+
test("should throw an error for invalid card ranks", () => {
34+
expect(() => getCardValue("X♣")).toThrow("Invalid card rank");
35+
expect(() => getCardValue("1♠")).toThrow("Invalid card rank");
36+
expect(() => getCardValue("Z♦")).toThrow("Invalid card rank");
37+
});

0 commit comments

Comments
 (0)