Skip to content

Commit 7480a6d

Browse files
committed
complete Sprint3.1 tasks
1 parent 42f55b8 commit 7480a6d

6 files changed

Lines changed: 140 additions & 3 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,10 @@ function assertEquals(actualOutput, targetOutput) {
4747
// Example: Identify Right Angles
4848
const right = getAngleType(90);
4949
assertEquals(right, "Right angle");
50+
assertEquals(getAngleType(0), "Invalid angle");
51+
assertEquals(getAngleType(-1), "Invalid angle");
52+
assertEquals(getAngleType(360), "Invalid angle");
53+
assertEquals(getAngleType(45), "Acute angle");
54+
assertEquals(getAngleType(100), "Obtuse angle");
55+
assertEquals(getAngleType(180), "Straight angle");
56+
assertEquals(getAngleType(200), "Reflex angle");

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function isProperFraction(numerator, denominator) {
1717
}
1818

1919

20-
The line below allows us to load the isProperFraction function into tests in other files.
20+
//The line below allows us to load the isProperFraction function into tests in other files.
2121
// This will be useful in the "rewrite tests with jest" step.
2222
module.exports = isProperFraction;
2323

@@ -34,3 +34,10 @@ function assertEquals(actualOutput, targetOutput) {
3434

3535
// Example: 1/2 is a proper fraction
3636
assertEquals(isProperFraction(1, 2), true);
37+
assertEquals(isProperFraction(2, 1), false);
38+
assertEquals(isProperFraction(2, 4), true);
39+
assertEquals(isProperFraction(10, 5), false);
40+
assertEquals(isProperFraction(-1, -2), true);
41+
assertEquals(isProperFraction(3, 0), false);
42+
assertEquals(isProperFraction(0, 5), true);
43+
assertEquals(isProperFraction(3, 3), false);

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

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

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

67+
}
2868
// The line below allows us to load the getCardValue function into tests in other files.
2969
// This will be useful in the "rewrite tests with jest" step.
3070
module.exports = getCardValue;
@@ -40,8 +80,13 @@ function assertEquals(actualOutput, targetOutput) {
4080
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4181
// Examples:
4282
assertEquals(getCardValue("9♠"), 9);
83+
assertEquals(getCardValue("A♠"), 11);
84+
assertEquals(getCardValue("J♣"), 10);
85+
assertEquals(getCardValue("Q♦"), 10);
86+
assertEquals(getCardValue("K♥"), 10);
87+
assertEquals(getCardValue("2♥"), 2);
88+
assertEquals(getCardValue("A♠"), 11);
4389

44-
// Handling invalid cards
4590
try {
4691
getCardValue("invalid");
4792

@@ -52,3 +97,4 @@ try {
5297
}
5398

5499
// What other invalid card cases can you think of?
100+
// A) 1♠, 11♠, AA♠, ♠♠, 10

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,34 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
1414
});
1515

1616
// Case 2: Right angle
17+
test(`should return "Right angle" when angle = 90`, () => {
18+
expect(getAngleType(90)).toEqual("Right angle");
19+
});
20+
1721
// Case 3: Obtuse angles
22+
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
23+
expect(getAngleType(91)).toEqual("Obtuse angle");
24+
expect(getAngleType(120)).toEqual("Obtuse angle");
25+
expect(getAngleType(179)).toEqual("Obtuse angle");
26+
});
27+
1828
// Case 4: Straight angle
29+
test(`should return "Straight angle" when angle = 180`, () => {
30+
expect(getAngleType(180)).toEqual("Straight angle");
31+
});
32+
1933
// Case 5: Reflex angles
34+
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
35+
expect(getAngleType(181)).toEqual("Reflex angle");
36+
expect(getAngleType(265)).toEqual("Reflex angle");
37+
expect(getAngleType(359)).toEqual("Reflex angle");
38+
});
39+
2040
// Case 6: Invalid angles
41+
test(`should return "Invalid angle" when angle is not a number or is outside the valid range`, () => {
42+
expect(getAngleType("not a number")).toEqual("Invalid angle");
43+
expect(getAngleType(-1)).toEqual("Invalid angle");
44+
expect(getAngleType(361)).toEqual("Invalid angle");
45+
expect(getAngleType(0)).toEqual("Invalid angle");
46+
expect(getAngleType(360)).toEqual("Invalid angle");
47+
});

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,29 @@ 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+
12+
13+
// (True) - Proper fractions
14+
test(`should return true when |numerator| < |denominator|`, () => {
15+
expect(isProperFraction(1, 2)).toEqual(true);
16+
expect(isProperFraction(-1, 2)).toEqual(true);
17+
expect(isProperFraction(1, -3)).toEqual(true);
18+
expect(isProperFraction(-1, -3)).toEqual(true);
19+
expect(isProperFraction(2, 4)).toEqual(true);
20+
});
21+
22+
// (False) - Improper fractions
23+
test(`should return false when |numerator| >= |denominator|`, () => {
24+
expect(isProperFraction(2, 1)).toEqual(false);
25+
expect(isProperFraction(-2, 1)).toEqual(false);
26+
expect(isProperFraction(2, -1)).toEqual(false);
27+
expect(isProperFraction(-2, -1)).toEqual(false);
28+
expect(isProperFraction(3, 3)).toEqual(false);
29+
});
30+
31+
// (True) - Zero numerator
32+
test(`should return true when numerator is zero and denominator is non-zero`, () => {
33+
expect(isProperFraction(0, 1)).toEqual(true);
34+
expect(isProperFraction(0, 5)).toEqual(true);
35+
expect(isProperFraction(0, -5)).toEqual(true);
36+
});

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
@@ -11,8 +11,32 @@ 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 the numeric value when given a number card`, () => {
15+
expect(getCardValue("2♠")).toEqual(2);
16+
expect(getCardValue("10♥")).toEqual(10);
17+
expect(getCardValue("7♦")).toEqual(7);
18+
expect(getCardValue("5♣")).toEqual(5);
19+
});
20+
1421
// Face Cards (J, Q, K)
22+
test(`Should return 10 when given a face card`, () => {
23+
expect(getCardValue("J♣")).toEqual(10);
24+
expect(getCardValue("Q♦")).toEqual(10);
25+
expect(getCardValue("K♠")).toEqual(10);
26+
expect(getCardValue("K♥")).toEqual(10);
27+
});
28+
1529
// Invalid Cards
30+
test(`Should throw an error when given an invalid card`, () => {
31+
expect(() => getCardValue("1♠")).toThrow("Error: Invalid card format");
32+
expect(() => getCardValue("11♠")).toThrow("Error: Invalid card format");
33+
expect(() => getCardValue("AA♠")).toThrow("Error: Invalid card format");
34+
expect(() => getCardValue("♠♠")).toThrow("Error: Invalid card format");
35+
expect(() => getCardValue("10")).toThrow("Error: Invalid card format");
36+
expect(() => getCardValue("A♠♠")).toThrow("Error: Invalid card format");
37+
expect(() => getCardValue("0♠")).toThrow("Error: Invalid card format");
38+
39+
});
1640

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

0 commit comments

Comments
 (0)