Skip to content

Commit e577f7f

Browse files
committed
implement and rewrite test exercises
1 parent 3372770 commit e577f7f

File tree

7 files changed

+123
-8
lines changed

7 files changed

+123
-8
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616

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

2127
// The line below allows us to load the getAngleType function into tests in other files.
@@ -35,3 +41,31 @@ function assertEquals(actualOutput, targetOutput) {
3541
// Example: Identify Right Angles
3642
const right = getAngleType(90);
3743
assertEquals(right, "Right angle");
44+
let acute = getAngleType(1);
45+
assertEquals(acute, "Acute angle");
46+
acute = getAngleType(45);
47+
assertEquals(acute, "Acute angle");
48+
acute = getAngleType(89);
49+
assertEquals(acute, "Acute angle");
50+
let obtuse = getAngleType(91);
51+
assertEquals(obtuse, "Obtuse angle");
52+
obtuse = getAngleType(100);
53+
assertEquals(obtuse, "Obtuse angle");
54+
obtuse = getAngleType(179);
55+
assertEquals(obtuse, "Obtuse angle");
56+
const straight = getAngleType(180);
57+
assertEquals(straight, "Straight angle");
58+
let reflex = getAngleType(200);
59+
assertEquals(reflex, "Reflex angle");
60+
reflex = getAngleType(243);
61+
assertEquals(reflex, "Reflex angle");
62+
reflex = getAngleType(359);
63+
assertEquals(reflex, "Reflex angle");
64+
let invalid = getAngleType(-11);
65+
assertEquals(invalid, "Invalid angle");
66+
invalid = getAngleType(0);
67+
assertEquals(invalid, "Invalid angle");
68+
invalid = getAngleType(360);
69+
assertEquals(invalid, "Invalid angle");
70+
invalid = getAngleType(371);
71+
assertEquals(invalid, "Invalid angle");

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
function isProperFraction(numerator, denominator) {
1414
// TODO: Implement this function
15+
if (Math.abs(numerator) < Math.abs(denominator)) return true;
16+
else return false;
1517
}
1618

1719
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -31,3 +33,9 @@ function assertEquals(actualOutput, targetOutput) {
3133

3234
// Example: 1/2 is a proper fraction
3335
assertEquals(isProperFraction(1, 2), true);
36+
assertEquals(isProperFraction(7, 4), false);
37+
assertEquals(isProperFraction(-8, 5), false);
38+
assertEquals(isProperFraction(9, 10), true);
39+
assertEquals(isProperFraction(-3, -6), true);
40+
assertEquals(isProperFraction(15, 11), false);
41+
assertEquals(isProperFraction(-17, -24), true);

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,23 @@
2323

2424
function getCardValue(card) {
2525
// TODO: Implement this function
26+
if (typeof card !== "string") {
27+
throw new Error("Invalid card");
28+
}
29+
const validSuits = ["♠", "♥", "♦", "♣"];
30+
const suit = card.slice(-1);
31+
const rank = card.slice(0, -1);
32+
if (!validSuits.includes(suit)) {
33+
throw new Error("Invalid card");
34+
} else if (rank === "A") return 11;
35+
else if (["J", "Q", "K"].includes(rank)) return 10;
36+
const number = Number(rank);
37+
if (number >= 2 && number <= 10) return number;
38+
throw new Error("Invalid card");
2639
}
2740

41+
module.exports = getCardValue;
42+
2843
// The line below allows us to load the getCardValue function into tests in other files.
2944
// This will be useful in the "rewrite tests with jest" step.
3045
module.exports = getCardValue;

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,41 @@ const getAngleType = require("../implement/1-get-angle-type");
66
// including boundary and invalid cases.
77

88
// Case 1: Acute angles
9-
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
9+
test(`should return "Acute angles" when (0 < angle < 90)`, () => {
1010
// Test various acute angles, including boundary cases
11-
expect(getAngleType(1)).toEqual("Acute angle");
12-
expect(getAngleType(45)).toEqual("Acute angle");
13-
expect(getAngleType(89)).toEqual("Acute angle");
11+
expect(getAngleType(1)).toBe("Acute angle");
12+
expect(getAngleType(45)).toBe("Acute angle");
13+
expect(getAngleType(89)).toBe("Acute angle");
1414
});
1515

1616
// Case 2: Right angle
17+
test(`should return "Right angle" when (angle === 90)`, () => {
18+
// Test various acute angles, including boundary cases
19+
expect(getAngleType(90)).toBe("Right angle");
20+
});
1721
// Case 3: Obtuse angles
22+
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
23+
// Test various acute angles, including boundary cases
24+
expect(getAngleType(97)).toBe("Obtuse angle");
25+
expect(getAngleType(129)).toBe("Obtuse angle");
26+
expect(getAngleType(165)).toBe("Obtuse angle");
27+
});
1828
// Case 4: Straight angle
29+
test(`should return "Straight angle" when (angle ==180)`, () => {
30+
// Test various acute angles, including boundary cases
31+
expect(getAngleType(180)).toBe("Straight angle");
32+
});
1933
// Case 5: Reflex angles
34+
test(`should return "Reflex angles" when (180 < angle < 360)`, () => {
35+
// Test various acute angles, including boundary cases
36+
expect(getAngleType(191)).toBe("Reflex angle");
37+
expect(getAngleType(250)).toBe("Reflex angle");
38+
expect(getAngleType(317)).toBe("Reflex angle");
39+
});
2040
// Case 6: Invalid angles
41+
test(`should return "Invalid angles" when (angle >= 360 or angle<= 0 )`, () => {
42+
// Test various acute angles, including boundary cases
43+
expect(getAngleType(0)).toBe("Invalid angle");
44+
expect(getAngleType(-45)).toBe("Invalid angle");
45+
expect(getAngleType(370)).toBe("Invalid angle");
46+
});

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,15 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
77
// Special case: numerator is zero
88
test(`should return false when denominator is zero`, () => {
99
expect(isProperFraction(1, 0)).toEqual(false);
10+
expect(isProperFraction(0, 1)).toEqual(true);
11+
expect(isProperFraction(0, -1)).toEqual(true);
12+
expect(isProperFraction(18, 1)).toEqual(false);
13+
expect(isProperFraction(7, 3)).toEqual(false);
14+
expect(isProperFraction(1, -2)).toEqual(true);
15+
expect(isProperFraction(-15, -9)).toEqual(false);
16+
expect(isProperFraction(-2, -6)).toEqual(true);
17+
expect(isProperFraction(-137, -71)).toEqual(false);
18+
expect(isProperFraction(-100, -189)).toEqual(true);
19+
expect(isProperFraction(27, 5)).toEqual(false);
20+
expect(isProperFraction(-29, 17)).toEqual(false);
1021
});

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@ const getCardValue = require("../implement/3-get-card-value");
88
test(`Should return 11 when given an ace card`, () => {
99
expect(getCardValue("A♠")).toEqual(11);
1010
});
11+
// Case 2: Number Cards (2-10)
12+
test(`Should return the numeric value for number cards`, () => {
13+
expect(getCardValue("2♠")).toEqual(2);
14+
expect(getCardValue("5♥")).toEqual(5);
15+
expect(getCardValue("9♦")).toEqual(9);
16+
expect(getCardValue("10♣")).toEqual(10);
17+
});
18+
19+
// Case 3: Face Cards (J, Q, K)
20+
test(`Should return 10 for face cards`, () => {
21+
expect(getCardValue("J♠")).toEqual(10);
22+
expect(getCardValue("Q♥")).toEqual(10);
23+
expect(getCardValue("K♦")).toEqual(10);
24+
});
25+
26+
// Case 4: Invalid Cards
27+
test(`Should throw "Invalid card" for invalid cards`, () => {
28+
expect(() => getCardValue("1♠")).toThrow("Invalid card");
29+
expect(() => getCardValue("B♠")).toThrow("Invalid card");
30+
expect(() => getCardValue("A$")).toThrow("Invalid card");
31+
expect(() => getCardValue("10X")).toThrow("Invalid card");
32+
});
1133

1234
// Suggestion: Group the remaining test data into these categories:
1335
// Number Cards (2-10)
@@ -17,4 +39,3 @@ test(`Should return 11 when given an ace card`, () => {
1739
// To learn how to test whether a function throws an error as expected in Jest,
1840
// please refer to the Jest documentation:
1941
// https://jestjs.io/docs/expect#tothrowerror
20-

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"keywords": [],
1010
"author": "Code Your Future",
1111
"license": "ISC",
12-
"dependencies": {
13-
"jest": "^29.7.0"
12+
"devDependencies": {
13+
"jest": "^30.2.0"
1414
}
15-
}
15+
}

0 commit comments

Comments
 (0)