Skip to content

Commit e12f625

Browse files
Complete all implementation and Jest tests for Sprint 3
1 parent 3372770 commit e12f625

File tree

6 files changed

+228
-38
lines changed

6 files changed

+228
-38
lines changed

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

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,19 @@
1515
// execute the code to ensure all tests pass.
1616

1717
function getAngleType(angle) {
18-
// TODO: Implement this function
18+
if (angle <= 0 || angle >= 360) {
19+
return "Invalid angle";
20+
} else if (angle < 90) {
21+
return "Acute angle";
22+
} else if (angle === 90) {
23+
return "Right angle";
24+
} else if (angle < 180) {
25+
return "Obtuse angle";
26+
} else if (angle === 180) {
27+
return "Straight angle";
28+
} else {
29+
return "Reflex angle";
30+
}
1931
}
2032

2133
// The line below allows us to load the getAngleType function into tests in other files.
@@ -31,7 +43,39 @@ function assertEquals(actualOutput, targetOutput) {
3143
);
3244
}
3345

34-
// TODO: Write tests to cover all cases, including boundary and invalid cases.
35-
// Example: Identify Right Angles
46+
// Tests to cover all cases, including boundary and invalid cases.
47+
console.log("Starting tests...");
48+
49+
// 1. Right angle
3650
const right = getAngleType(90);
3751
assertEquals(right, "Right angle");
52+
53+
// 2. Acute angle
54+
const acute = getAngleType(45);
55+
assertEquals(acute, "Acute angle");
56+
57+
// 3. Obtuse angle
58+
const obtuse = getAngleType(120);
59+
assertEquals(obtuse, "Obtuse angle");
60+
61+
// 4. Straight angle
62+
const straight = getAngleType(180);
63+
assertEquals(straight, "Straight angle");
64+
65+
// 5. Reflex angle
66+
const reflex = getAngleType(250);
67+
assertEquals(reflex, "Reflex angle");
68+
69+
// 6. Invalid angle - zero
70+
const invalidZero = getAngleType(0);
71+
assertEquals(invalidZero, "Invalid angle");
72+
73+
// 7. Invalid angle - negative
74+
const invalidNegative = getAngleType(-50);
75+
assertEquals(invalidNegative, "Invalid angle");
76+
77+
// 8. Invalid angle - 360 or more
78+
const invalidTooBig = getAngleType(360);
79+
assertEquals(invalidTooBig, "Invalid angle");
80+
81+
console.log("All tests completed!");

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,52 @@
44

55
// Assumption: The parameters are valid numbers (not NaN or Infinity).
66

7-
// Note: If you are unfamiliar with proper fractions, please look up its mathematical definition.
8-
97
// Acceptance criteria:
108
// After you have implemented the function, write tests to cover all the cases, and
119
// execute the code to ensure all tests pass.
1210

1311
function isProperFraction(numerator, denominator) {
14-
// TODO: Implement this function
12+
if (denominator === 0) {
13+
return false;
14+
}
15+
return Math.abs(numerator) < Math.abs(denominator);
1516
}
1617

1718
// The line below allows us to load the isProperFraction function into tests in other files.
18-
// This will be useful in the "rewrite tests with jest" step.
1919
module.exports = isProperFraction;
2020

21-
// Here's our helper again
21+
// Helper function
2222
function assertEquals(actualOutput, targetOutput) {
2323
console.assert(
2424
actualOutput === targetOutput,
2525
`Expected ${actualOutput} to equal ${targetOutput}`
2626
);
2727
}
2828

29-
// TODO: Write tests to cover all cases.
30-
// What combinations of numerators and denominators should you test?
29+
console.log("Starting tests for isProperFraction...");
3130

32-
// Example: 1/2 is a proper fraction
31+
// 1. Proper fraction: 1/2
3332
assertEquals(isProperFraction(1, 2), true);
33+
34+
// 2. Improper fraction: numerator greater than denominator
35+
assertEquals(isProperFraction(3, 2), false);
36+
37+
// 3. Improper fraction: numerator equals denominator
38+
assertEquals(isProperFraction(5, 5), false);
39+
40+
// 4. Proper fraction with negative numerator
41+
assertEquals(isProperFraction(-1, 4), true);
42+
43+
// 5. Proper fraction with negative denominator
44+
assertEquals(isProperFraction(2, -5), true);
45+
46+
// 6. Improper fraction with negative numerator
47+
assertEquals(isProperFraction(-7, 3), false);
48+
49+
// 7. Zero numerator — 0/5 is proper because 0 < 5
50+
assertEquals(isProperFraction(0, 5), true);
51+
52+
// 8. Zero denominator — invalid, return false
53+
assertEquals(isProperFraction(1, 0), false);
54+
55+
console.log("All tests completed!");

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

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,91 @@
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("Invalid card format");
27+
}
28+
29+
const validSuits = ["♠", "♥", "♦", "♣"];
30+
const validRanks = [
31+
"A",
32+
"2",
33+
"3",
34+
"4",
35+
"5",
36+
"6",
37+
"7",
38+
"8",
39+
"9",
40+
"10",
41+
"J",
42+
"Q",
43+
"K",
44+
];
45+
46+
const suit = card.slice(-1);
47+
const rank = card.slice(0, -1);
48+
49+
if (!validSuits.includes(suit) || !validRanks.includes(rank)) {
50+
throw new Error("Invalid card format");
51+
}
52+
53+
if (rank === "A") {
54+
return 11;
55+
} else if (rank === "J" || rank === "Q" || rank === "K") {
56+
return 10;
57+
} else {
58+
return Number(rank);
59+
}
2660
}
2761

2862
// The line below allows us to load the getCardValue function into tests in other files.
29-
// This will be useful in the "rewrite tests with jest" step.
3063
module.exports = getCardValue;
3164

32-
// Helper functions to make our assertions easier to read.
65+
// Helper function for value assertions
3366
function assertEquals(actualOutput, targetOutput) {
3467
console.assert(
3568
actualOutput === targetOutput,
3669
`Expected ${actualOutput} to equal ${targetOutput}`
3770
);
3871
}
3972

40-
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
41-
// Examples:
73+
// Helper function to assert that an error is thrown
74+
function assertThrowsError(invalidInput) {
75+
let errorThrown = false;
76+
try {
77+
getCardValue(invalidInput);
78+
} catch (e) {
79+
errorThrown = true;
80+
}
81+
console.assert(
82+
errorThrown,
83+
`Expected an error to be thrown for input: "${invalidInput}"`
84+
);
85+
}
86+
87+
console.log("Starting tests for getCardValue...");
88+
89+
// --- Number cards ---
90+
assertEquals(getCardValue("2♥"), 2);
4291
assertEquals(getCardValue("9♠"), 9);
92+
assertEquals(getCardValue("10♥"), 10);
93+
94+
// --- Face cards ---
95+
assertEquals(getCardValue("J♣"), 10);
96+
assertEquals(getCardValue("Q♦"), 10);
97+
assertEquals(getCardValue("K♦"), 10);
4398

44-
// Handling invalid cards
45-
try {
46-
getCardValue("invalid");
99+
// --- Ace card ---
100+
assertEquals(getCardValue("A♠"), 11);
47101

48-
// This line will not be reached if an error is thrown as expected
49-
console.error("Error was not thrown for invalid card");
50-
} catch (e) {}
102+
// --- Invalid cards ---
103+
assertThrowsError("invalid"); // random string
104+
assertThrowsError("10"); // missing suit
105+
assertThrowsError("A"); // missing suit
106+
assertThrowsError("♠"); // missing rank
107+
assertThrowsError("1♠"); // invalid rank
108+
assertThrowsError("11♠"); // invalid rank
109+
assertThrowsError("AX"); // invalid suit
110+
assertThrowsError(""); // empty string
51111

52-
// What other invalid card cases can you think of?
112+
console.log("All tests completed!");

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,41 @@
22
// We will use the same function, but write tests for it using Jest in this file.
33
const getAngleType = require("../implement/1-get-angle-type");
44

5-
// TODO: Write tests in Jest syntax to cover all cases/outcomes,
6-
// including boundary and invalid cases.
7-
85
// Case 1: Acute angles
96
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
10-
// Test various acute angles, including boundary cases
117
expect(getAngleType(1)).toEqual("Acute angle");
128
expect(getAngleType(45)).toEqual("Acute angle");
139
expect(getAngleType(89)).toEqual("Acute angle");
1410
});
1511

1612
// Case 2: Right angle
13+
test(`should return "Right angle" when angle is 90`, () => {
14+
expect(getAngleType(90)).toEqual("Right angle");
15+
});
16+
1717
// Case 3: Obtuse angles
18+
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
19+
expect(getAngleType(91)).toEqual("Obtuse angle");
20+
expect(getAngleType(120)).toEqual("Obtuse angle");
21+
expect(getAngleType(179)).toEqual("Obtuse angle");
22+
});
23+
1824
// Case 4: Straight angle
25+
test(`should return "Straight angle" when angle is 180`, () => {
26+
expect(getAngleType(180)).toEqual("Straight angle");
27+
});
28+
1929
// Case 5: Reflex angles
30+
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
31+
expect(getAngleType(181)).toEqual("Reflex angle");
32+
expect(getAngleType(250)).toEqual("Reflex angle");
33+
expect(getAngleType(359)).toEqual("Reflex angle");
34+
});
35+
2036
// Case 6: Invalid angles
37+
test(`should return "Invalid angle" when angle is <= 0 or >= 360`, () => {
38+
expect(getAngleType(0)).toEqual("Invalid angle");
39+
expect(getAngleType(-50)).toEqual("Invalid angle");
40+
expect(getAngleType(360)).toEqual("Invalid angle");
41+
expect(getAngleType(400)).toEqual("Invalid angle");
42+
});

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,37 @@
22
// We will use the same function, but write tests for it using Jest in this file.
33
const isProperFraction = require("../implement/2-is-proper-fraction");
44

5-
// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
5+
// 1. Proper fractions (absolute numerator < absolute denominator)
6+
test(`should return true for proper fractions (positive and negative)`, () => {
7+
expect(isProperFraction(1, 2)).toEqual(true);
8+
expect(isProperFraction(-1, 4)).toEqual(true);
9+
expect(isProperFraction(3, -5)).toEqual(true);
10+
expect(isProperFraction(-2, -7)).toEqual(true);
11+
});
12+
13+
// 2. Improper fractions (absolute numerator > absolute denominator)
14+
test(`should return false for improper fractions where numerator is greater than denominator`, () => {
15+
expect(isProperFraction(3, 2)).toEqual(false);
16+
expect(isProperFraction(-5, 4)).toEqual(false);
17+
expect(isProperFraction(7, -3)).toEqual(false);
18+
});
19+
20+
// 3. Improper fractions (absolute numerator == absolute denominator)
21+
test(`should return false when numerator equals denominator`, () => {
22+
expect(isProperFraction(5, 5)).toEqual(false);
23+
expect(isProperFraction(-5, 5)).toEqual(false);
24+
expect(isProperFraction(5, -5)).toEqual(false);
25+
});
26+
27+
// 4. Special case: numerator is zero
28+
test(`should return true when numerator is zero and denominator is not zero`, () => {
29+
expect(isProperFraction(0, 5)).toEqual(true);
30+
expect(isProperFraction(0, -5)).toEqual(true);
31+
});
632

7-
// Special case: numerator is zero
33+
// 5. Special case: denominator is zero
834
test(`should return false when denominator is zero`, () => {
935
expect(isProperFraction(1, 0)).toEqual(false);
36+
expect(isProperFraction(-1, 0)).toEqual(false);
37+
expect(isProperFraction(0, 0)).toEqual(false);
1038
});

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,33 @@
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-
// TODO: Write tests in Jest syntax to cover all possible outcomes.
6-
75
// Case 1: Ace (A)
86
test(`Should return 11 when given an ace card`, () => {
97
expect(getCardValue("A♠")).toEqual(11);
108
});
119

12-
// Suggestion: Group the remaining test data into these categories:
13-
// Number Cards (2-10)
14-
// Face Cards (J, Q, K)
15-
// Invalid Cards
10+
// Case 2: Number Cards (2-10)
11+
test(`Should return the numeric value for number cards`, () => {
12+
expect(getCardValue("2♥")).toEqual(2);
13+
expect(getCardValue("9♠")).toEqual(9);
14+
expect(getCardValue("10♦")).toEqual(10);
15+
});
1616

17-
// To learn how to test whether a function throws an error as expected in Jest,
18-
// please refer to the Jest documentation:
19-
// https://jestjs.io/docs/expect#tothrowerror
17+
// Case 3: Face Cards (J, Q, K)
18+
test(`Should return 10 for face cards (J, Q, K)`, () => {
19+
expect(getCardValue("J♣")).toEqual(10);
20+
expect(getCardValue("Q♦")).toEqual(10);
21+
expect(getCardValue("K♥")).toEqual(10);
22+
});
2023

24+
// Case 4: Invalid Cards
25+
test(`Should throw an error for invalid cards`, () => {
26+
expect(() => getCardValue("invalid")).toThrow();
27+
expect(() => getCardValue("10")).toThrow();
28+
expect(() => getCardValue("A")).toThrow();
29+
expect(() => getCardValue("♠")).toThrow();
30+
expect(() => getCardValue("1♠")).toThrow();
31+
expect(() => getCardValue("11♠")).toThrow();
32+
expect(() => getCardValue("AX")).toThrow();
33+
expect(() => getCardValue("")).toThrow();
34+
});

0 commit comments

Comments
 (0)