Skip to content

Commit 662a7eb

Browse files
Complete Sprint 3: 1-implement-and-rewrite-tests
1 parent 3685fe5 commit 662a7eb

File tree

6 files changed

+132
-17
lines changed

6 files changed

+132
-17
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,27 @@
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+
}
21+
22+
if (angle === 90) {
23+
return "Right angle";
24+
}
25+
26+
if (angle > 0 && angle < 90) {
27+
return "Acute angle";
28+
}
29+
30+
if (angle > 90 && angle < 180) {
31+
return "Obtuse angle";
32+
}
33+
34+
if (angle === 180) {
35+
return "Straight angle";
36+
}
37+
38+
return "Reflex angle";
1939
}
2040

2141
// The line below allows us to load the getAngleType function into tests in other files.
@@ -33,5 +53,29 @@ function assertEquals(actualOutput, targetOutput) {
3353

3454
// TODO: Write tests to cover all cases, including boundary and invalid cases.
3555
// Example: Identify Right Angles
36-
const right = getAngleType(90);
37-
assertEquals(right, "Right angle");
56+
// Acute Angles:
57+
assertEquals(getAngleType(1), "Acute angle");
58+
assertEquals(getAngleType(45), "Acute angle");
59+
assertEquals(getAngleType(89), "Acute angle");
60+
61+
//Right Angles:
62+
assertEquals(getAngleType(90), "Right angle");
63+
64+
//Obtuse Angles:
65+
assertEquals(getAngleType(91), "Obtuse angle");
66+
assertEquals(getAngleType(120), "Obtuse angle");
67+
assertEquals(getAngleType(179), "Obtuse angle");
68+
69+
//Straight Angles:
70+
assertEquals(getAngleType(180), "Straight angle");
71+
72+
//Reflex Angles:
73+
assertEquals(getAngleType(181), "Reflex angle");
74+
assertEquals(getAngleType(270), "Reflex angle");
75+
assertEquals(getAngleType(359), "Reflex angle");
76+
77+
// Invalid Angles
78+
assertEquals(getAngleType(0), "Invalid angle");
79+
assertEquals(getAngleType(360), "Invalid angle");
80+
assertEquals(getAngleType(-10), "Invalid angle");
81+
assertEquals(getAngleType(720), "Invalid angle");

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

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

1313
function isProperFraction(numerator, denominator) {
14-
// TODO: Implement this function
14+
if (denominator === 0) return false;
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.
@@ -30,4 +31,28 @@ function assertEquals(actualOutput, targetOutput) {
3031
// What combinations of numerators and denominators should you test?
3132

3233
// Example: 1/2 is a proper fraction
34+
35+
//Proper Fractions
3336
assertEquals(isProperFraction(1, 2), true);
37+
assertEquals(isProperFraction(2, 5), true);
38+
39+
//Equal values (Not proper fractions)
40+
assertEquals(isProperFraction(2, 2), false);
41+
42+
//Improper Fractions
43+
assertEquals(isProperFraction(5, 3), false);
44+
45+
//Negative Numerator
46+
assertEquals(isProperFraction(-1, 3), true);
47+
48+
//Negative Denominator
49+
assertEquals(isProperFraction(1, -3), true);
50+
51+
//Both Negative Numerator and Denominator
52+
assertEquals(isProperFraction(-2, -5), true);
53+
54+
//Zero Numerator
55+
assertEquals(isProperFraction(0, 5), true);
56+
57+
//Zero Denominator
58+
assertEquals(isProperFraction(1, 0), false);

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

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,29 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25-
// TODO: Implement this function
26-
}
25+
if (typeof card !== "string" || card.length < 2){
26+
throw new Error("Invalid card");
27+
}
28+
29+
const suits = ["♠", "♥", "♦", "♣"];
30+
const suit = card.slice(-1);
31+
const rank = card.slice(0,-1);
32+
33+
if (!suits.includes(suit)) {
34+
throw new Error("Invalid card");
35+
}
36+
37+
if (rank === "A") return 11;
38+
if (["J", "Q", "K"].includes(rank)) return 10;
39+
40+
const numericValue = Number(rank);
41+
42+
if (numericValue >= 2 && numericValue <= 10) {
43+
return numericValue;
44+
}
45+
46+
throw new Error("Invalid card");
47+
}
2748

2849
// The line below allows us to load the getCardValue function into tests in other files.
2950
// This will be useful in the "rewrite tests with jest" step.
@@ -41,12 +62,38 @@ function assertEquals(actualOutput, targetOutput) {
4162
// Examples:
4263
assertEquals(getCardValue("9♠"), 9);
4364

65+
// Ace
66+
assertEquals(getCardValue("A♠"), 11);
67+
68+
// Face cards
69+
assertEquals(getCardValue("J♥"), 10);
70+
assertEquals(getCardValue("Q♦"), 10);
71+
assertEquals(getCardValue("K♣"), 10);
72+
73+
// Numeric Cards
74+
assertEquals(getCardValue("2♠"), 2);
75+
assertEquals(getCardValue("10♦"), 10);
76+
4477
// Handling invalid cards
4578
try {
4679
getCardValue("invalid");
4780

4881
// This line will not be reached if an error is thrown as expected
4982
console.error("Error was not thrown for invalid card");
50-
} catch (e) {}
83+
} catch (e) {
84+
console.log("Correctly threw error for invalid card");
85+
}
5186

5287
// What other invalid card cases can you think of?
88+
89+
// Invliad suit
90+
try { getCardValue("A?"); console.error("No error thrown"); } catch(e) {}
91+
92+
// Invalid rank
93+
try { getCardValue("1♠"); console.error("No error thrown"); } catch(e) {}
94+
95+
// Missing suit
96+
try { getCardValue("A"); console.error("No error thrown"); } catch(e) {}
97+
98+
// Extra characters
99+
try { getCardValue("AAA♠"); console.error("No error thrown"); } catch(e) {}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
1313
expect(getAngleType(89)).toEqual("Acute angle");
1414
});
1515

16-
// Case 2: Right angle
16+
// Case 2: Right angles
1717
test('should return "Right angle" when angle is 90', () => {
1818
expect(getAngleType(90)).toEqual("Right angle");
1919
});

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,43 @@ 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: denominator is zero
7+
// Case 1: Special case: denominator is zero
88
test(`should return false when denominator is zero`, () => {
99
expect(isProperFraction(1, 0)).toEqual(false);
1010
});
1111

12-
// Numerator is zero
12+
// Case 2: Numerator is zero
1313
test(`should return true when numerator is zero and denominator is non-zero`, () => {
1414
expect(isProperFraction(0, 1)).toEqual(true);
1515
});
1616

17-
//Proper fractions
17+
// Case 3: Proper fractions
1818
test("should return true for proper fractions", () => {
1919
expect(isProperFraction(1,2)).toEqual(true);
2020
expect(isProperFraction(2,5)).toEqual(true);
2121
})
2222

23-
//Equal Numerator and Denominator
23+
// Case 4: Equal Numerator and Denominator
2424
test("should return false when numerator equals denominator", () => {
2525
expect(isProperFraction(2,2)).toEqual(false);
2626
})
2727

28-
//Improper fractions
28+
// Case 5: Improper fractions
2929
test("should return false for improper fractions", () => {
3030
expect(isProperFraction(5,3)).toEqual(false);
3131
})
3232

33-
//Negative numerator
33+
// Case 6: Negative numerator
3434
test("should return true when numerator is negative but absolute value is smaller", () => {
3535
expect(isProperFraction(-1, 3)).toEqual(true);
3636
})
3737

38-
//Negative denominator
38+
// Case 7: Negative denominator
3939
test("should return true when denominator is negative but absolute value is larger", () => {
4040
expect(isProperFraction(1, -3)).toEqual(true);
4141
})
4242

43-
//Both Negative
43+
// Case 8: Both Negative
4444
test("should return true when both numerator and denominator are negative and proper", () => {
4545
expect(isProperFraction(-2, -5)).toEqual(true);
4646
})

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ test("should throw error for missing suit", () => {
3939
test("should throw error for completely invalid string", () => {
4040
expect(() => getCardValue("invalid")).toThrow();
4141
});
42-
4342
// Suggestion: Group the remaining test data into these categories:
4443
// Number Cards (2-10)
4544
// Face Cards (J, Q, K)

0 commit comments

Comments
 (0)