Skip to content

Commit 0e37d5b

Browse files
Complete Sprint 3 implement and rewrite tests
1 parent 3372770 commit 0e37d5b

File tree

6 files changed

+247
-52
lines changed

6 files changed

+247
-52
lines changed

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

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,63 @@
88
// - "Reflex angle" for angles greater than 180° and less than 360°
99
// - "Invalid angle" for angles outside the valid range.
1010

11-
// Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.)
12-
13-
// Acceptance criteria:
14-
// After you have implemented the function, write tests to cover all the cases, and
15-
// execute the code to ensure all tests pass.
11+
// Assumption: The parameter is a valid number.
1612

1713
function getAngleType(angle) {
18-
// TODO: Implement this function
14+
if (angle > 0 && angle < 90) {
15+
return "Acute angle";
16+
} else if (angle === 90) {
17+
return "Right angle";
18+
} else if (angle > 90 && angle < 180) {
19+
return "Obtuse angle";
20+
} else if (angle === 180) {
21+
return "Straight angle";
22+
} else if (angle > 180 && angle < 360) {
23+
return "Reflex angle";
24+
} else {
25+
return "Invalid angle";
26+
}
1927
}
2028

21-
// The line below allows us to load the getAngleType function into tests in other files.
22-
// This will be useful in the "rewrite tests with jest" step.
2329
module.exports = getAngleType;
2430

25-
// This helper function is written to make our assertions easier to read.
26-
// If the actual output matches the target output, the test will pass
31+
// Helper function for testing
2732
function assertEquals(actualOutput, targetOutput) {
2833
console.assert(
2934
actualOutput === targetOutput,
3035
`Expected ${actualOutput} to equal ${targetOutput}`
3136
);
3237
}
3338

34-
// TODO: Write tests to cover all cases, including boundary and invalid cases.
35-
// Example: Identify Right Angles
36-
const right = getAngleType(90);
37-
assertEquals(right, "Right angle");
39+
// ======================
40+
// Tests
41+
// ======================
42+
43+
// Acute angles
44+
assertEquals(getAngleType(45), "Acute angle");
45+
assertEquals(getAngleType(1), "Acute angle");
46+
assertEquals(getAngleType(89), "Acute angle");
47+
48+
// Right angle
49+
assertEquals(getAngleType(90), "Right angle");
50+
51+
// Obtuse angles
52+
assertEquals(getAngleType(120), "Obtuse angle");
53+
assertEquals(getAngleType(179), "Obtuse angle");
54+
assertEquals(getAngleType(91), "Obtuse angle");
55+
56+
// Straight angle
57+
assertEquals(getAngleType(180), "Straight angle");
58+
59+
// Reflex angles
60+
assertEquals(getAngleType(200), "Reflex angle");
61+
assertEquals(getAngleType(359), "Reflex angle");
62+
assertEquals(getAngleType(181), "Reflex angle");
63+
64+
// Invalid angles
65+
assertEquals(getAngleType(0), "Invalid angle");
66+
assertEquals(getAngleType(360), "Invalid angle");
67+
assertEquals(getAngleType(-10), "Invalid angle");
68+
assertEquals(getAngleType(400), "Invalid angle");
69+
70+
console.log("All tests passed!");

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

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,56 @@
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-
9-
// Acceptance criteria:
10-
// After you have implemented the function, write tests to cover all the cases, and
11-
// execute the code to ensure all tests pass.
7+
// A proper fraction is when |numerator| < |denominator|
128

139
function isProperFraction(numerator, denominator) {
14-
// TODO: Implement this function
10+
11+
// A fraction cannot have denominator 0
12+
if (denominator === 0) {
13+
return false;
14+
}
15+
16+
// Check if absolute value of numerator is less than denominator
17+
if (Math.abs(numerator) < Math.abs(denominator)) {
18+
return true;
19+
} else {
20+
return false;
21+
}
1522
}
1623

17-
// 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.
24+
1925
module.exports = isProperFraction;
2026

21-
// Here's our helper again
27+
// Helper function for testing
2228
function assertEquals(actualOutput, targetOutput) {
2329
console.assert(
2430
actualOutput === targetOutput,
2531
`Expected ${actualOutput} to equal ${targetOutput}`
2632
);
2733
}
2834

29-
// TODO: Write tests to cover all cases.
30-
// What combinations of numerators and denominators should you test?
3135

32-
// Example: 1/2 is a proper fraction
36+
// Tests
37+
// ========================
38+
39+
// Proper fractions
3340
assertEquals(isProperFraction(1, 2), true);
41+
assertEquals(isProperFraction(3, 4), true);
42+
assertEquals(isProperFraction(-1, 2), true);
43+
assertEquals(isProperFraction(2, -5), true);
44+
assertEquals(isProperFraction(-3, -7), true);
45+
46+
// Not proper (equal values)
47+
assertEquals(isProperFraction(5, 5), false);
48+
assertEquals(isProperFraction(-4, -4), false);
49+
50+
// Not proper (numerator larger)
51+
assertEquals(isProperFraction(7, 3), false);
52+
assertEquals(isProperFraction(-9, 4), false);
53+
assertEquals(isProperFraction(10, -2), false);
54+
55+
// Denominator zero (invalid fraction)
56+
assertEquals(isProperFraction(1, 0), false);
57+
assertEquals(isProperFraction(0, 0), false);
58+
59+
console.log("All tests passed!");
Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,101 @@
11
// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck
2-
2+
//
3+
// ORIGINAL QUESTION:
34
// Implement a function getCardValue, when given a string representing a playing card,
45
// should return the numerical value of the card.
5-
6+
//
67
// A valid card string will contain a rank followed by the suit.
78
// The rank can be one of the following strings:
89
// "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
910
// The suit can be one of the following emojis:
1011
// "♠", "♥", "♦", "♣"
1112
// For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦".
12-
13+
//
1314
// When the card is an ace ("A"), the function should return 11.
1415
// When the card is a face card ("J", "Q", "K"), the function should return 10.
1516
// When the card is a number card ("2" to "10"), the function should return its numeric value.
16-
17+
//
1718
// When the card string is invalid (not following the above format), the function should
1819
// throw an error.
19-
20+
//
2021
// Acceptance criteria:
2122
// After you have implemented the function, write tests to cover all the cases, and
2223
// execute the code to ensure all tests pass.
2324

2425
function getCardValue(card) {
25-
// TODO: Implement this function
26+
// 1. Separate the rank from the suit emoji
27+
const rank = card.slice(0, -1);
28+
const suit = card.slice(-1);
29+
30+
// 2. Validate the suit
31+
const validSuits = ["♠", "♥", "♦", "♣"];
32+
if (!validSuits.includes(suit)) {
33+
throw new Error("Invalid suit");
34+
}
35+
36+
// 3. Calculate value based on the rank
37+
if (rank === "A") {
38+
return 11;
39+
}
40+
41+
if (rank === "J" || rank === "Q" || rank === "K") {
42+
return 10;
43+
}
44+
45+
// 4. Handle number cards (2-10)
46+
const numericRank = parseInt(rank, 10);
47+
48+
// numericRank.toString() === rank makes sure rank was really a clean number string
49+
// Example: parseInt("9", 10) -> 9, "9" === "9" ✅
50+
// Example: parseInt("9x", 10) -> 9, "9" === "9x" ❌ (so invalid)
51+
if (numericRank >= 2 && numericRank <= 10 && numericRank.toString() === rank) {
52+
return numericRank;
53+
}
54+
55+
// 5. If it reaches here, the rank was invalid
56+
throw new Error("Invalid rank");
2657
}
2758

28-
// 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.
59+
// Export for other test files (Jest step later)
3060
module.exports = getCardValue;
3161

32-
// Helper functions to make our assertions easier to read.
62+
// Helper function for testing (console.assert style)
3363
function assertEquals(actualOutput, targetOutput) {
3464
console.assert(
3565
actualOutput === targetOutput,
3666
`Expected ${actualOutput} to equal ${targetOutput}`
3767
);
3868
}
3969

40-
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
41-
// Examples:
70+
// =======================
71+
// TESTS
72+
// =======================
73+
74+
// Test Aces and Face Cards
75+
assertEquals(getCardValue("A♠"), 11);
76+
assertEquals(getCardValue("J♥"), 10);
77+
assertEquals(getCardValue("Q♦"), 10);
78+
assertEquals(getCardValue("K♣"), 10);
79+
80+
// Test Numbers
4281
assertEquals(getCardValue("9♠"), 9);
82+
assertEquals(getCardValue("10♥"), 10);
83+
assertEquals(getCardValue("2♣"), 2);
4384

44-
// Handling invalid cards
85+
// Test Invalid Inputs (should throw errors)
4586
try {
4687
getCardValue("invalid");
88+
console.error("Error was not thrown for 'invalid'");
89+
} catch (e) {}
4790

48-
// This line will not be reached if an error is thrown as expected
49-
console.error("Error was not thrown for invalid card");
91+
try {
92+
getCardValue("1♠"); // There is no 1 card, only A,2..10,J,Q,K
93+
console.error("Error was not thrown for '1♠'");
94+
} catch (e) {}
95+
96+
try {
97+
getCardValue("A?"); // invalid suit
98+
console.error("Error was not thrown for 'A?'");
5099
} catch (e) {}
51100

52-
// What other invalid card cases can you think of?
101+
console.log("Tests complete!");

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,33 @@ 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(200)).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 outside the valid range`, () => {
42+
expect(getAngleType(0)).toEqual("Invalid angle");
43+
expect(getAngleType(360)).toEqual("Invalid angle");
44+
expect(getAngleType(-1)).toEqual("Invalid angle");
45+
expect(getAngleType(361)).toEqual("Invalid angle");
46+
});

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,45 @@
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.
65

7-
// Special case: numerator is zero
6+
// Case 1: Denominator is zero
87
test(`should return false when denominator is zero`, () => {
98
expect(isProperFraction(1, 0)).toEqual(false);
9+
expect(isProperFraction(0, 0)).toEqual(false);
10+
});
11+
12+
13+
// Case 2: Numerator is zero
14+
test(`should return true when numerator is zero and denominator is not zero`, () => {
15+
expect(isProperFraction(0, 5)).toEqual(true);
16+
expect(isProperFraction(0, -10)).toEqual(true);
17+
});
18+
19+
20+
// Case 3: Positive proper fractions
21+
test(`should return true for positive proper fractions`, () => {
22+
expect(isProperFraction(1, 2)).toEqual(true);
23+
expect(isProperFraction(3, 4)).toEqual(true);
24+
});
25+
26+
27+
// Case 4: Positive improper fractions
28+
test(`should return false for positive improper fractions`, () => {
29+
expect(isProperFraction(5, 2)).toEqual(false);
30+
expect(isProperFraction(4, 4)).toEqual(false);
31+
});
32+
33+
34+
// Case 5: Negative proper fractions
35+
test(`should return true for negative proper fractions`, () => {
36+
expect(isProperFraction(-1, 2)).toEqual(true);
37+
expect(isProperFraction(2, -5)).toEqual(true);
38+
expect(isProperFraction(-3, -7)).toEqual(true);
39+
});
40+
41+
42+
// Case 6: Negative improper fractions
43+
test(`should return false for negative improper fractions`, () => {
44+
expect(isProperFraction(-5, 2)).toEqual(false);
45+
expect(isProperFraction(9, -4)).toEqual(false);
1046
});

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

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,44 @@
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.
65

7-
// Case 1: Ace (A)
6+
// Case 1: Ace
87
test(`Should return 11 when given an ace card`, () => {
98
expect(getCardValue("A♠")).toEqual(11);
9+
expect(getCardValue("A♥")).toEqual(11);
1010
});
1111

12-
// Suggestion: Group the remaining test data into these categories:
13-
// Number Cards (2-10)
14-
// Face Cards (J, Q, K)
15-
// Invalid Cards
1612

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
13+
// Case 2: Number Cards (2-10)
14+
test(`Should return correct number for number cards`, () => {
15+
expect(getCardValue("2♣")).toEqual(2);
16+
expect(getCardValue("9♠")).toEqual(9);
17+
expect(getCardValue("10♦")).toEqual(10);
18+
});
19+
20+
21+
// Case 3: Face Cards (J, Q, K)
22+
test(`Should return 10 for face cards`, () => {
23+
expect(getCardValue("J♠")).toEqual(10);
24+
expect(getCardValue("Q♥")).toEqual(10);
25+
expect(getCardValue("K♦")).toEqual(10);
26+
});
27+
28+
29+
// Case 4: Invalid Cards - Invalid suit
30+
test(`Should throw error for invalid suit`, () => {
31+
expect(() => getCardValue("A?")).toThrow();
32+
});
33+
34+
35+
// Case 5: Invalid Cards - Invalid rank
36+
test(`Should throw error for invalid rank`, () => {
37+
expect(() => getCardValue("1♠")).toThrow();
38+
expect(() => getCardValue("11♣")).toThrow();
39+
});
40+
2041

42+
// Case 6: Completely invalid string
43+
test(`Should throw error for completely invalid input`, () => {
44+
expect(() => getCardValue("invalid")).toThrow();
45+
});

0 commit comments

Comments
 (0)