Skip to content

Commit cdbec51

Browse files
committed
implement solutions and rewrite tests with jest
1 parent 49fe3af commit cdbec51

File tree

6 files changed

+208
-3
lines changed

6 files changed

+208
-3
lines changed

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

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

11+
const { assertEquals, isProperFraction } = require("./2-is-proper-fraction");
12+
1113
// Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.)
1214

1315
// Acceptance criteria:
@@ -16,6 +18,18 @@
1618

1719
function getAngleType(angle) {
1820
// TODO: Implement this function
21+
if (angle > 0 && angle < 90)
22+
return "Acute angle";
23+
else if (angle === 90)
24+
return "Right angle";
25+
else if (angle > 90 && angle < 180)
26+
return "obtuse angle";
27+
else if (angle === 180)
28+
return "Straight angle"
29+
else if (angle > 180 && angle < 360)
30+
return "Reflex angle";
31+
else
32+
return "Invalid angle";
1933
}
2034

2135
// The line below allows us to load the getAngleType function into tests in other files.
@@ -35,3 +49,34 @@ function assertEquals(actualOutput, targetOutput) {
3549
// Example: Identify Right Angles
3650
const right = getAngleType(90);
3751
assertEquals(right, "Right angle");
52+
53+
const acute = getAngleType(45);
54+
assertEquals(acute, "Acute angle");
55+
56+
const obtuse = getAngleType(120);
57+
assertEquals(obtuse, "obtuse angle");
58+
59+
const straight = getAngleType(180);
60+
assertEquals(straight, "Straight angle");
61+
62+
const relex = getAngleType(270);
63+
assertEquals(relex, "Reflex angle");
64+
65+
const invalid = getAngleType(400);
66+
assertEquals(invalid, "Invalid angle");
67+
assertEquals(isProperFraction(2, 2), false);
68+
assertEquals(isProperFraction(0, 0), false);
69+
// Case 3: Obtuse angles
70+
test(`should return "obtuse angle" when (90 < angle < 180)`, () => {
71+
expect(getAngleType(120)).toEqual("obtuse angle");
72+
expect(getAngleType(179)).toEqual("obtuse angle");
73+
expect(getAngleType(179.99)).toEqual("obtuse angle");
74+
expect(getAngleType(179.999)).toEqual("obtuse angle");
75+
expect(getAngleType(179.9999)).toEqual("obtuse angle");
76+
expect(getAngleType(179.99999)).toEqual("obtuse angle");
77+
expect(getAngleType(179.999999)).toEqual("obtuse angle");
78+
expect(getAngleType(179.9999999)).toEqual("obtuse angle");
79+
expect(getAngleType(179.99999999)).toEqual("obtuse angle");
80+
expect(getAngleType(179.999999999)).toEqual("obtuse angle");
81+
82+
});

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@
1212

1313
function isProperFraction(numerator, denominator) {
1414
// TODO: Implement this function
15+
if (denominator === 0){
16+
return false;
17+
}
18+
19+
return numerator < denominator;
20+
1521
}
22+
exports.isProperFraction = isProperFraction;
1623

1724
// The line below allows us to load the isProperFraction function into tests in other files.
1825
// This will be useful in the "rewrite tests with jest" step.
@@ -25,9 +32,17 @@ function assertEquals(actualOutput, targetOutput) {
2532
`Expected ${actualOutput} to equal ${targetOutput}`
2633
);
2734
}
35+
exports.assertEquals = assertEquals;
2836

2937
// TODO: Write tests to cover all cases.
3038
// What combinations of numerators and denominators should you test?
3139

3240
// Example: 1/2 is a proper fraction
3341
assertEquals(isProperFraction(1, 2), true);
42+
assertEquals(isProperFraction(2, 1), false);
43+
assertEquals(isProperFraction(0, 1), true);
44+
assertEquals(isProperFraction(1, 0), false);
45+
assertEquals(isProperFraction(-1, 2), true);
46+
assertEquals(isProperFraction(1, -2), false);
47+
assertEquals(isProperFraction(-1, -2), false);
48+

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

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,34 @@
2323

2424
function getCardValue(card) {
2525
// TODO: Implement this function
26+
const rank = card.slice(0, -1); // get the rank by slicing off the last character (the suit)
27+
const suit = card.slice(-1); // get the suit by taking the last character
28+
29+
const validSuits = ["♠", "♥", "♦", "♣"]; // Define valid suits
30+
if (!validSuits.includes(suit)) { // check if the suit is valid
31+
throw new Error("Invalid card"); // If the suit is not valid, throw an error
32+
}
33+
34+
const faceCardValues = { // Define the values for face cards and ace
35+
"A": 11, // Ace is worth 11
36+
"J": 10, // Jack is worth 10
37+
"Q": 10, // Queen is worth 10
38+
"K": 10 // King is worth 10
39+
};
40+
if (faceCardValues[rank]) { // Check if the rank is a face card or ace
41+
return faceCardValues[rank]; // If it is, return the corresponding value
42+
}
43+
44+
const numericValue = parseInt(rank); // Try to parse the rank as a number
45+
if (numericValue >= 2 && numericValue <= 10) { // check if the numeric value is between 2 and 10
46+
return numericValue; // If it is, return the numeric value
47+
}
48+
49+
throw new Error("Invalid card"); // If the rank is not valid (not a face card, ace, or number between 2 and 10), throw an error
2650
}
51+
console.log(getCardValue("A♠")); // 11
52+
console.log(getCardValue("2♥")); // 2
53+
console.log(getCardValue("J♣")); // 10
2754

2855
// The line below allows us to load the getCardValue function into tests in other files.
2956
// This will be useful in the "rewrite tests with jest" step.
@@ -40,13 +67,30 @@ function assertEquals(actualOutput, targetOutput) {
4067
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4168
// Examples:
4269
assertEquals(getCardValue("9♠"), 9);
70+
assertEquals(getCardValue("A♥"), 11);
71+
assertEquals(getCardValue("J♣"), 10);
72+
assertEquals(getCardValue("Q♦"), 10);
73+
assertEquals(getCardValue("K♠"), 10);
74+
assertEquals(getCardValue("10♥"), 10);
75+
assertEquals(getCardValue("2♦"), 2);
76+
4377

4478
// Handling invalid cards
45-
try {
46-
getCardValue("invalid");
4779

80+
try {
81+
getCardValue("1 ♠"); // Invalid rank
4882
// This line will not be reached if an error is thrown as expected
4983
console.error("Error was not thrown for invalid card");
5084
} catch (e) {}
5185

86+
5287
// What other invalid card cases can you think of?
88+
try {
89+
getCardValue("11♠"); // Invalid rank
90+
console.error("Error was not thrown for invalid card");
91+
} catch (e) {}
92+
93+
try {
94+
getCardValue("h♠"); // Valid card
95+
console.error("Error was thrown for valid card");
96+
} catch (e) {}

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

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

1616
// Case 2: Right angle
17-
// Case 3: Obtuse angles
17+
test('should return "Right angle" when angle is exactly 90', () => {
18+
expect(getAngleType(90)).toEqual("Right angle");
19+
});
1820
// Case 4: Straight angle
21+
test(`should return "Straight angle" when angle is exactly 180`, () => {
22+
expect(getAngleType(180)).toEqual("Straight angle");
23+
});
24+
1925
// Case 5: Reflex angles
26+
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
27+
expect(getAngleType(181)).toEqual("Reflex angle");
28+
expect(getAngleType(190)).toEqual("Reflex angle");
29+
expect(getAngleType(200)).toEqual("Reflex angle");
30+
expect(getAngleType(300)).toEqual("Reflex angle");
31+
expect(getAngleType(359)).toEqual("Reflex angle");
32+
expect(getAngleType(359.90)).toEqual("Reflex angle");
33+
expect(getAngleType(359.99)).toEqual("Reflex angle");
34+
expect(getAngleType(359.999)).toEqual("Reflex angle");
35+
expect(getAngleType(359.9999)).toEqual("Reflex angle");
36+
expect(getAngleType(359.99999)).toEqual("Reflex angle");
37+
expect(getAngleType(359.999999)).toEqual("Reflex angle");
38+
expect(getAngleType(359.9999999)).toEqual("Reflex angle");
39+
expect(getAngleType(359.99999999)).toEqual("Reflex angle");
40+
expect(getAngleType(359.999999999)).toEqual("Reflex angle");
41+
42+
43+
});
2044
// Case 6: Invalid angles
45+
test(`should return "Invalid angle" when angle (angle <= 0 or angle >= 360)`, () => {
46+
expect(getAngleType(-1)).toEqual("Invalid angle");
47+
expect(getAngleType(0)).toEqual("Invalid angle");
48+
expect(getAngleType(-0.1)).toEqual("Invalid angle");
49+
expect(getAngleType(-10)).toEqual("Invalid angle");
50+
expect(getAngleType(360)).toEqual("Invalid angle");
51+
expect(getAngleType("string")).toEqual("Invalid angle");
52+
expect(getAngleType(null)).toEqual("Invalid angle");
53+
expect(getAngleType(undefined)).toEqual("Invalid angle");
54+
expect(getAngleType(NaN)).toEqual("Invalid angle");
55+
56+
});

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,45 @@ 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+
test(`should return true in normal cases where numerator < denominator`, () => {
13+
expect(isProperFraction(1, 2)).toEqual(true);
14+
});
15+
16+
test(`should return false when numerator >= denominator`, () => {
17+
expect(isProperFraction(2, 1)).toEqual(false);
18+
expect(isProperFraction(2, 2)).toEqual(false);
19+
});
20+
21+
test('should return true when numerator is negative and denominator is positive', () => {
22+
expect(isProperFraction(-1, 2)).toEqual(true);
23+
expect(isProperFraction(-2, 1)).toEqual(true);
24+
expect(isProperFraction(-2, 2)).toEqual(true)
25+
26+
});
27+
28+
test(`should return false when numerator is positive and denominator is negative`, () => {
29+
expect(isProperFraction(1, -2)).toEqual(false);
30+
expect(isProperFraction(2, -1)).toEqual(false);
31+
32+
});
33+
34+
test(`should return false when numerator and denominator are both negative`, () => {
35+
expect(isProperFraction(-1, -2)).toEqual(false);
36+
expect(isProperFraction(-2, -1)).toEqual(false);
37+
expect(isProperFraction(-2, -2)).toEqual(false);
38+
});
39+
40+
test(`should return true when numerator is zero and denominator is positive`, () => {
41+
expect(isProperFraction(0, 1)).toEqual(true);
42+
expect(isProperFraction(0, 2)).toEqual(true);
43+
44+
});
45+
46+
test(`should return false when numerator is zero and denominator is negative`, () => {
47+
expect(isProperFraction(0, -1)).toEqual(false);
48+
expect(isProperFraction(0, -2)).toEqual(false);
49+
50+
});
51+
52+

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,33 @@ test(`Should return 11 when given an ace card`, () => {
99
expect(getCardValue("A♠")).toEqual(11);
1010
});
1111

12+
1213
// Suggestion: Group the remaining test data into these categories:
1314
// Number Cards (2-10)
15+
test(`Should return the correct value for number cards (2-10)`, () => {
16+
expect(getCardValue("2♠")).toEqual(2);
17+
expect(getCardValue("3♥")).toEqual(3);
18+
expect(getCardValue("4♦")).toEqual(4);
19+
expect(getCardValue("5♣")).toEqual(5);
20+
expect(getCardValue("6♠")).toEqual(6);
21+
expect(getCardValue("7♥")).toEqual(7);
22+
expect(getCardValue("8♦")).toEqual(8);
23+
expect(getCardValue("9♣")).toEqual(9);
24+
expect(getCardValue("10♠")).toEqual(10);
25+
})
1426
// Face Cards (J, Q, K)
27+
test(`Should return 10 for face cards (J, Q, K)`, () => {
28+
expect(getCardValue("J♥")).toEqual(10);
29+
expect(getCardValue("Q♦")).toEqual(10);
30+
expect(getCardValue("K♣")).toEqual(10);
31+
});
32+
1533
// Invalid Cards
34+
test(`Should throw an error for invalid cards`, () => {
35+
expect(() => getCardValue("1♠")).toThrow("Invalid card");
36+
expect(() => getCardValue("11♣")).toThrow("Invalid card");
37+
expect(() => getCardValue("A♦")).toThrow("Invalid card");
38+
});
1639

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

0 commit comments

Comments
 (0)