Skip to content

Commit c1a67c1

Browse files
committed
Completed all task required for Sprint 3 Implement and rewrite data
1 parent 3372770 commit c1a67c1

File tree

6 files changed

+259
-8
lines changed

6 files changed

+259
-8
lines changed

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

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,24 @@
1414
// After you have implemented the function, write tests to cover all the cases, and
1515
// execute the code to ensure all tests pass.
1616

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

34+
2135
// The line below allows us to load the getAngleType function into tests in other files.
2236
// This will be useful in the "rewrite tests with jest" step.
2337
module.exports = getAngleType;
@@ -33,5 +47,55 @@ function assertEquals(actualOutput, targetOutput) {
3347

3448
// TODO: Write tests to cover all cases, including boundary and invalid cases.
3549
// Example: Identify Right Angles
50+
51+
// Acute angle test
52+
const acute1 = getAngleType(45);
53+
assertEquals(acute1, "Acute angle");
54+
55+
const acute2 = getAngleType(1);
56+
assertEquals(acute2, "Acute angle");
57+
58+
const acute3 = getAngleType(89.999);
59+
assertEquals(acute3, "Acute angle");
60+
61+
// Right angle test
3662
const right = getAngleType(90);
3763
assertEquals(right, "Right angle");
64+
65+
// Obtuse angle test
66+
const obtuse1 = getAngleType(120);
67+
assertEquals(obtuse1, "Obtuse angle");
68+
69+
const obtuse2 = getAngleType(179.999);
70+
assertEquals(obtuse2, "Obtuse angle");
71+
72+
// Straight angle test
73+
const straight = getAngleType(180);
74+
assertEquals(straight, "Straight angle");
75+
76+
// Reflex angle test
77+
const reflex1 = getAngleType(200);
78+
assertEquals(reflex1, "Reflex angle");
79+
80+
const reflex2 = getAngleType(359.999);
81+
assertEquals(reflex2, "Reflex angle");
82+
83+
// Invalid angle test
84+
const invalid1 = getAngleType(0);
85+
assertEquals(invalid1, "Invalid angle");
86+
87+
const invalid2 = getAngleType(360);
88+
assertEquals(invalid2, "Invalid angle");
89+
90+
const invalid3 = getAngleType(-10);
91+
assertEquals(invalid3, "Invalid angle");
92+
93+
const invalid4 = getAngleType(400);
94+
assertEquals(invalid4, "Invalid angle");
95+
96+
97+
console.log("All tests executed.");
98+
99+
// Note: If no assertion errors are thrown, all tests have passed succesfully.
100+
// i feel there is a better way or cleaner way to write the test, but for now,
101+
// this is a simple way to cover all cases.

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,27 @@
55
// Assumption: The parameters are valid numbers (not NaN or Infinity).
66

77
// Note: If you are unfamiliar with proper fractions, please look up its mathematical definition.
8+
// A proper fraction means: The top number (numerator) is smaller than the bottom number (denominator).
9+
// e.g. 1/2, 3/5. 5/2 would not be a proper fraction.
10+
// Numerator < Denominator
811

912
// Acceptance criteria:
1013
// After you have implemented the function, write tests to cover all the cases, and
1114
// execute the code to ensure all tests pass.
1215

13-
function isProperFraction(numerator, denominator) {
1416
// TODO: Implement this function
17+
function isProperFraction(numerator, denominator) {
18+
// A fraction cannot have denominator 0
19+
if (denominator === 0) {
20+
return false;
21+
}
22+
23+
// A proper fraction means numerator is smaller than denominator
24+
if (numerator < denominator) {
25+
return true;
26+
} else {
27+
return false;
28+
}
1529
}
1630

1731
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -28,6 +42,28 @@ function assertEquals(actualOutput, targetOutput) {
2842

2943
// TODO: Write tests to cover all cases.
3044
// What combinations of numerators and denominators should you test?
45+
// I should test proper fractions, equal numbers, improper fractions, zero numerator,
46+
// zero denominator and negative numbers.
3147

3248
// Example: 1/2 is a proper fraction
3349
assertEquals(isProperFraction(1, 2), true);
50+
assertEquals(isProperFraction(3, 5), true);
51+
52+
// Equal numbers (not proper)
53+
assertEquals(isProperFraction(4, 4), false);
54+
55+
// Improper fractions
56+
assertEquals(isProperFraction(5, 2), false);
57+
assertEquals(isProperFraction(10, 3), false);
58+
59+
// Zero numerator (0 is less than denominator)
60+
assertEquals(isProperFraction(0, 5), true);
61+
62+
// Zero denominator (invalid fraction)
63+
assertEquals(isProperFraction(5, 0), false);
64+
65+
// Negative numbers
66+
assertEquals(isProperFraction(-1, 5), true);
67+
assertEquals(isProperFraction(1, -5), false);
68+
69+
console.log("All tests executed.");

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

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,47 @@
2121
// After you have implemented the function, write tests to cover all the cases, and
2222
// execute the code to ensure all tests pass.
2323

24+
// TODO: Implement this function
2425
function getCardValue(card) {
25-
// TODO: Implement this function
26+
// Valid suits
27+
const validSuits = ["♠", "♥", "♦", "♣"];
28+
29+
// Card must be at least 2 characters (e.g., "A♠")
30+
if (typeof card !== "string" || card.length < 2) {
31+
throw new Error("Invalid card");
32+
}
33+
34+
// Suit is always the last character
35+
const suit = card.slice(-1);
36+
37+
// Rank is everything except the last character
38+
const rank = card.slice(0, -1);
39+
40+
// Validate suit
41+
if (!validSuits.includes(suit)) {
42+
throw new Error("Invalid card");
43+
}
44+
45+
// Handle Ace
46+
if (rank === "A") {
47+
return 11;
48+
}
49+
50+
// Handle face cards
51+
if (rank === "J" || rank === "Q" || rank === "K") {
52+
return 10;
53+
}
54+
55+
// Handle number cards (2–10)
56+
const number = Number(rank);
57+
58+
if (number >= 2 && number <= 10) {
59+
return number;
60+
}
61+
62+
// If nothing matched → invalid
63+
throw new Error("Invalid card");
64+
2665
}
2766

2867
// The line below allows us to load the getCardValue function into tests in other files.
@@ -38,15 +77,47 @@ function assertEquals(actualOutput, targetOutput) {
3877
}
3978

4079
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
80+
// Test: Ace, face cards, number cards, invalid suit, invalid rank, completely invalid string.
4181
// Examples:
82+
// Number cards:
4283
assertEquals(getCardValue("9♠"), 9);
84+
assertEquals(getCardValue("2♠"), 2);
85+
assertEquals(getCardValue("10♦"), 10);
86+
87+
// Face card:
88+
assertEquals(getCardValue("J♣"), 10);
89+
assertEquals(getCardValue("Q♦"), 10);
90+
assertEquals(getCardValue("K♠"), 10);
91+
92+
// Ace
93+
assertEquals(getCardValue("A♥"), 11);
4394

4495
// Handling invalid cards
4596
try {
4697
getCardValue("invalid");
98+
console.error("Error was not thrown for invalid string");
99+
} catch (e) {}
100+
101+
// Invalid rank
102+
try {
103+
getCardValue("1♠");
104+
console.error("Error wasnot thrown for invalid rank");
105+
} catch (e) {}
47106

48-
// This line will not be reached if an error is thrown as expected
49-
console.error("Error was not thrown for invalid card");
107+
// Invalid suit
108+
try {
109+
getCardValue("A?");
110+
console.error("Error wasnot thrown for invalid suit");
50111
} catch (e) {}
51112

113+
// Missing suit
114+
try {
115+
getCardValue("A");
116+
console.error("Error wasnot thrown for missing suit");
117+
} catch (e) {}
118+
119+
console.log("All tests executed.");
120+
121+
52122
// What other invalid card cases can you think of?
123+
// Test: Ace, face cards, number cards, invalid suit, invalid rank, completely invalid string.

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 is 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(100)).toEqual("Obtuse angle");
24+
expect(getAngleType(150)).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 is 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(200)).toEqual("Reflex angle");
36+
expect(getAngleType(300)).toEqual("Reflex angle");
37+
expect(getAngleType(359)).toEqual("Reflex angle");
38+
});
39+
2040
// Case 6: Invalid angles
41+
test(`should return "Invalid angle" for angles outside valid range`, () => {
42+
expect(getAngleType(0)).toEqual("Invalid angle");
43+
expect(getAngleType(360)).toEqual("Invalid angle");
44+
expect(getAngleType(-10)).toEqual("Invalid angle");
45+
expect(getAngleType(400)).toEqual("Invalid angle");
46+
});

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,38 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
55
// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
66

77
// Special case: numerator is zero
8-
test(`should return false when denominator is zero`, () => {
8+
test(`should return false when denominator is 0`, () => {
99
expect(isProperFraction(1, 0)).toEqual(false);
10+
expect(isProperFraction(0, 0)).toEqual(false);
1011
});
12+
13+
// Zero numerator
14+
test(`should return true when numerator is 0 and denominator positive`, () => {
15+
expect(isProperFraction(0, 5)).toEqual(true);
16+
});
17+
18+
// Proper fractions (positive)
19+
test(`should return true for proper positive fractions`, () => {
20+
expect(isProperFraction(1, 2)).toEqual(true);
21+
expect(isProperFraction(3, 5)).toEqual(true);
22+
});
23+
24+
// Improper fractions (positive)
25+
test(`should return false for improper positive fractions`, () => {
26+
expect(isProperFraction(5, 2)).toEqual(false);
27+
expect(isProperFraction(10, 3)).toEqual(false);
28+
expect(isProperFraction(4, 4)).toEqual(false); // numerator = denominator
29+
});
30+
31+
// Negative fractions
32+
test(`should handle negative numerators`, () => {
33+
expect(isProperFraction(-1, 5)).toEqual(true);
34+
});
35+
36+
test(`should handle negative denominators`, () => {
37+
expect(isProperFraction(1, -5)).toEqual(false);
38+
});
39+
40+
test(`should handle both numerator and denominator negative`, () => {
41+
expect(isProperFraction(-2, -5)).toEqual(true);
42+
});

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

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

12+
// Case 2: Number cards (2-10)
13+
test(`Should return the correct number for number cards`, () => {
14+
expect(getCardValue("2♠")).toEqual(2);
15+
expect(getCardValue("5♥")).toEqual(5);
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 error for invalid cards`, () => {
28+
expect(() => getCardValue("1♠")).toThrow("Invalid card"); // invalid rank
29+
expect(() => getCardValue("A?")).toThrow("Invalid card"); // invalid suit
30+
expect(() => getCardValue("10♠♠")).toThrow("Invalid card"); // extra character
31+
expect(() => getCardValue("")).toThrow("Invalid card"); // empty string
32+
expect(() => getCardValue("banana")).toThrow("Invalid card"); // completely invalid
33+
});
34+
1235
// Suggestion: Group the remaining test data into these categories:
1336
// Number Cards (2-10)
1437
// Face Cards (J, Q, K)
1538
// Invalid Cards
1639

1740
// To learn how to test whether a function throws an error as expected in Jest,
1841
// please refer to the Jest documentation:
19-
// https://jestjs.io/docs/expect#tothrowerror
20-
42+
// https://jestjs.io/docs/expect#tothrowerror

0 commit comments

Comments
 (0)