Skip to content

Commit e16389e

Browse files
Complete sprint-3/implementandrewritetests
1 parent 124ae45 commit e16389e

File tree

6 files changed

+208
-5
lines changed

6 files changed

+208
-5
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@
1515
// execute the code to ensure all tests pass.
1616

1717
function getAngleType(angle) {
18-
// TODO: Implement this function
18+
if (typeof angle !== "number" || angle <= 0 || angle >= 360)
19+
return "Invalid angle";
20+
if (angle === 90) return "Right angle";
21+
if (angle < 90) return "Acute angle";
22+
if (angle > 90 && angle < 180) return "Obtuse angle";
23+
if (angle === 180) return "Straight angle";
24+
return "Reflex angle";
1925
}
2026

2127
// The line below allows us to load the getAngleType function into tests in other files.
@@ -33,5 +39,24 @@ function assertEquals(actualOutput, targetOutput) {
3339

3440
// TODO: Write tests to cover all cases, including boundary and invalid cases.
3541
// Example: Identify Right Angles
42+
3643
const right = getAngleType(90);
3744
assertEquals(right, "Right angle");
45+
46+
const acute = getAngleType(45);
47+
assertEquals(acute, "Acute angle");
48+
49+
const obtuse = getAngleType(120);
50+
assertEquals(obtuse, "Obtuse angle");
51+
52+
const straight = getAngleType(180);
53+
assertEquals(straight, "Straight angle");
54+
55+
const reflex = getAngleType(260);
56+
assertEquals(reflex, "Reflex angle");
57+
58+
const invalid1 = getAngleType(400);
59+
assertEquals(invalid1, "Invalid angle");
60+
61+
const invalid2 = getAngleType("400");
62+
assertEquals(invalid2, "Invalid angle");

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
// execute the code to ensure all tests pass.
1212

1313
function isProperFraction(numerator, denominator) {
14-
// TODO: Implement this function
14+
numerator = Math.abs(numerator);
15+
denominator = Math.abs(denominator);
16+
if (numerator < denominator) return true;
17+
else if (numerator > denominator) return false;
18+
else if (numerator === denominator) return false;
1519
}
1620

1721
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -31,3 +35,23 @@ function assertEquals(actualOutput, targetOutput) {
3135

3236
// Example: 1/2 is a proper fraction
3337
assertEquals(isProperFraction(1, 2), true);
38+
39+
// Example: 5/2 is not a proper fraction
40+
const improperFraction = isProperFraction(5, 2);
41+
assertEquals(improperFraction, false);
42+
43+
// Example: -4/7 is a proper fraction
44+
const negativeFraction = isProperFraction(-4, 7);
45+
assertEquals(negativeFraction, true);
46+
47+
// Example: 3/3 is not a proper fraction
48+
const equalFraction = isProperFraction(3, 3);
49+
assertEquals(equalFraction, false);
50+
51+
// Example: 4/-7 is a proper fraction
52+
const negativeFraction1 = isProperFraction(4, -7);
53+
assertEquals(negativeFraction1, true);
54+
55+
// Example: -4/-7 is a proper fraction
56+
const negativeFraction2 = isProperFraction(4, 7);
57+
assertEquals(negativeFraction2, true);

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
if (!["♠", "♣", "♦", "♥"].includes(card.slice(-1)))
26+
throw new Error("Invalid card rank.");
27+
const rank = card.slice(0, card.length - 1);
28+
if (rank === "A") return 11;
29+
if (["10", "J", "Q", "K"].includes(rank)) return 10;
30+
if (["2", "3", "4", "5", "6", "7", "8", "9"].includes(rank))
31+
return Number(rank);
32+
throw new Error("Invalid card rank.");
2633
}
2734

2835
// The line below allows us to load the getCardValue function into tests in other files.
@@ -41,6 +48,15 @@ function assertEquals(actualOutput, targetOutput) {
4148
// Examples:
4249
assertEquals(getCardValue("9♠"), 9);
4350

51+
const fiveOfHearts = getCardValue("5♥");
52+
assertEquals(fiveOfHearts, 5);
53+
54+
const faceCard = getCardValue("J♣");
55+
assertEquals(faceCard, 10);
56+
57+
const aceCard = getCardValue("A♦");
58+
assertEquals(aceCard, 11);
59+
4460
// Handling invalid cards
4561
try {
4662
getCardValue("invalid");
@@ -50,3 +66,28 @@ try {
5066
} catch (e) {}
5167

5268
// What other invalid card cases can you think of?
69+
try {
70+
getCardValue("9K");
71+
72+
console.log("Error was not thrown for invalid card");
73+
} catch (error) {}
74+
try {
75+
getCardValue("");
76+
77+
console.log("Error was not thrown for invalid card");
78+
} catch (error) {}
79+
try {
80+
getCardValue("ABC");
81+
82+
console.log("Error was not thrown for invalid card");
83+
} catch (error) {}
84+
try {
85+
getCardValue("A");
86+
87+
console.log("Error was not thrown for invalid card");
88+
} catch (error) {}
89+
try {
90+
getCardValue("JK");
91+
92+
console.log("Error was not thrown for invalid card");
93+
} catch (error) {}

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

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

1616
// Case 2: Right angle
17+
test(`should return "Acute angle" when (angle = 90)`, () => {
18+
expect(getAngleType(90)).toEqual("Right angle");
19+
});
20+
1721
// Case 3: Obtuse angles
22+
test("should identify obtuse angle (90° < angle < 180°)", () => {
23+
expect(getAngleType(91)).toEqual("Obtuse angle");
24+
expect(getAngleType(130)).toEqual("Obtuse angle");
25+
expect(getAngleType(179)).toEqual("Obtuse angle");
26+
});
27+
1828
// Case 4: Straight angle
29+
test("should identify straight angle (angle = 180°)", () => {
30+
expect(getAngleType(180)).toEqual("Straight angle");
31+
});
32+
1933
// Case 5: Reflex angles
34+
test("should identify reflex angle (180° < angle < 360)", () => {
35+
expect(getAngleType(181)).toEqual("Reflex angle");
36+
expect(getAngleType(280)).toEqual("Reflex angle");
37+
expect(getAngleType(359)).toEqual("Reflex angle");
38+
});
39+
2040
// Case 6: Invalid angles
41+
test("should identify invalid angle (0 > angle > 360)", () => {
42+
expect(getAngleType(0)).toEqual("Invalid angle");
43+
expect(getAngleType(361)).toEqual("Invalid angle");
44+
});

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

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,69 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
44

55
// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
66

7-
// Special case: numerator is zero
7+
// Special case: denominator is zero
88
test(`should return false when denominator is zero`, () => {
99
expect(isProperFraction(1, 0)).toEqual(false);
1010
});
11+
12+
test("should return true when numerator is zero and denominator is non-zero", () => {
13+
expect(isProperFraction(0, 5)).toEqual(true);
14+
});
15+
16+
test("should return false when both numerator and denominator are zero", () => {
17+
expect(isProperFraction(0, 0)).toEqual(false);
18+
});
19+
20+
// Case 2: Identify Improper Fractions:
21+
test("should return false for improper fraction", () => {
22+
expect(isProperFraction(2, 3)).toEqual(true);
23+
});
24+
25+
// Case 3: Identify Negative Fractions:
26+
test("should return true for proper negative fraction", () => {
27+
expect(isProperFraction(-3, 6)).toEqual(true);
28+
});
29+
30+
test("should return false for improper negative fraction", () => {
31+
expect(isProperFraction(-5, 2)).toEqual(false);
32+
});
33+
34+
test("should return true for proper fraction with negative denominator", () => {
35+
expect(isProperFraction(2, -5)).toEqual(true);
36+
});
37+
38+
test("should return false for improper fraction with negative denominator", () => {
39+
expect(isProperFraction(7, -3)).toEqual(false);
40+
});
41+
42+
// Case 4: Identify Equal Numerator and Denominator:
43+
test("should return false for improper fraction (numerator === denominator)", () => {
44+
expect(isProperFraction(3, 3)).toEqual(false);
45+
});
46+
47+
// Case 5: Identify both Numerator and Denominator as negative
48+
test("should return true when both numerator and denominator are negative and proper", () => {
49+
expect(isProperFraction(-2, -5)).toEqual(true);
50+
});
51+
52+
test("should return false when both numerator and denominator are negative and improper", () => {
53+
expect(isProperFraction(-6, -3)).toEqual(false);
54+
});
55+
56+
// Case 6: Identify both Numerator and Denominator as decimal
57+
test("should return true for proper decimal fractions", () => {
58+
expect(isProperFraction(1.5, 2.5)).toEqual(true);
59+
});
60+
61+
test("should return false for improper decimal fractions", () => {
62+
expect(isProperFraction(2.5, 1.5)).toEqual(false);
63+
});
64+
65+
// Case 7: Identify both Numerator and Denominator as large numbers
66+
test("should return true for large proper fractions", () => {
67+
expect(isProperFraction(100, 1000)).toEqual(true);
68+
});
69+
70+
test("should return false for large improper fractions", () => {
71+
expect(isProperFraction(1000, 100)).toEqual(false);
72+
});

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,34 @@ const getCardValue = require("../implement/3-get-card-value");
77
// Case 1: Ace (A)
88
test(`Should return 11 when given an ace card`, () => {
99
expect(getCardValue("A♠")).toEqual(11);
10+
expect(getCardValue("A♣")).toEqual(11);
11+
expect(getCardValue("A♦")).toEqual(11);
12+
expect(getCardValue("A♥")).toEqual(11);
13+
});
14+
15+
// Case 2: Handle Number Cards (2-10):
16+
test("should return the appropriate number from 2 to 10", () => {
17+
expect(getCardValue("2♠")).toEqual(2);
18+
expect(getCardValue("3♠")).toEqual(3);
19+
expect(getCardValue("4♠")).toEqual(4);
20+
expect(getCardValue("5♠")).toEqual(5);
21+
expect(getCardValue("6♠")).toEqual(6);
22+
expect(getCardValue("7♠")).toEqual(7);
23+
expect(getCardValue("8♠")).toEqual(8);
24+
expect(getCardValue("9♠")).toEqual(9);
25+
expect(getCardValue("10♠")).toEqual(10);
26+
});
27+
// Case 3: Handle Face Cards (J, Q, K):
28+
test("should return 10 for face cards", () => {
29+
expect(getCardValue("J♠")).toEqual(10);
30+
expect(getCardValue("Q♠")).toEqual(10);
31+
expect(getCardValue("K♠")).toEqual(10);
32+
});
33+
34+
// Case 4: Handle Invalid Cards:
35+
test("Should return 'Invalid card rank.' for invalid cards", () => {
36+
expect(() => getCardValue("KJ")).toThrow("Invalid card rank.");
37+
expect(() => getCardValue("AK")).toThrow("Invalid card rank.");
1038
});
1139

1240
// Suggestion: Group the remaining test data into these categories:
@@ -17,4 +45,3 @@ test(`Should return 11 when given an ace card`, () => {
1745
// To learn how to test whether a function throws an error as expected in Jest,
1846
// please refer to the Jest documentation:
1947
// https://jestjs.io/docs/expect#tothrowerror
20-

0 commit comments

Comments
 (0)