Skip to content

Commit c40152a

Browse files
committed
implement-and-rewrite-tests completed
1 parent 6bbd0e6 commit c40152a

File tree

5 files changed

+204
-6
lines changed

5 files changed

+204
-6
lines changed

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,19 @@
1515
// execute the code to ensure all tests pass.
1616

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

2133
// The line below allows us to load the getAngleType function into tests in other files.
@@ -35,3 +47,32 @@ function assertEquals(actualOutput, targetOutput) {
3547
// Example: Identify Right Angles
3648
const right = getAngleType(90);
3749
assertEquals(right, "Right angle");
50+
51+
// Test Acute angle
52+
const acute = getAngleType(45);
53+
assertEquals(acute, "Acute angle");
54+
55+
// Test Obtuse angle
56+
const obtuse = getAngleType(135);
57+
assertEquals(obtuse, "Obtuse angle");
58+
59+
// Test Straight angle
60+
const straight = getAngleType(180);
61+
assertEquals(straight, "Straight angle");
62+
63+
// Test Reflex angle
64+
const reflex = getAngleType(270);
65+
assertEquals(reflex, "Reflex angle");
66+
67+
// Test Invalid angles
68+
const zero = getAngleType(0);
69+
assertEquals(zero, "Invalid angle");
70+
71+
const negative = getAngleType(-45);
72+
assertEquals(negative, "Invalid angle");
73+
74+
const fullCircle = getAngleType(360);
75+
assertEquals(fullCircle, "Invalid angle");
76+
77+
const over360 = getAngleType(400);
78+
assertEquals(over360, "Invalid angle");

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

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

1313
function isProperFraction(numerator, denominator) {
14-
// TODO: Implement this function
14+
return numerator > 0 && denominator > 0 && numerator < denominator;
1515
}
1616

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

3232
// Example: 1/2 is a proper fraction
3333
assertEquals(isProperFraction(1, 2), true);
34+
35+
// Test improper fractions
36+
assertEquals(isProperFraction(3, 2), false);
37+
assertEquals(isProperFraction(5, 5), false);
38+
39+
// Test negative numbers
40+
assertEquals(isProperFraction(-1, 2), false);
41+
assertEquals(isProperFraction(1, -2), false);
42+
43+
// Test zero
44+
assertEquals(isProperFraction(0, 2), false);
45+
assertEquals(isProperFraction(1, 0), false);
46+
47+
// Test other proper fractions
48+
assertEquals(isProperFraction(2, 3), true);
49+
assertEquals(isProperFraction(7, 10), true);

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

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,39 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
if (typeof card !== 'string') {
26+
throw new Error('Invalid card: must be a string');
27+
}
28+
29+
const suits = ['♠', '♥', '♦', '♣'];
30+
const ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];
31+
32+
let rank, suit;
33+
if (card.length === 2) {
34+
rank = card[0];
35+
suit = card[1];
36+
} else if (card.length === 3 && card.startsWith('10')) {
37+
rank = '10';
38+
suit = card[2];
39+
} else {
40+
throw new Error('Invalid card format');
41+
}
42+
43+
if (!suits.includes(suit)) {
44+
throw new Error('Invalid suit');
45+
}
46+
47+
if (!ranks.includes(rank)) {
48+
throw new Error('Invalid rank');
49+
}
50+
51+
if (rank === 'A') {
52+
return 11;
53+
} else if (['J', 'Q', 'K'].includes(rank)) {
54+
return 10;
55+
} else {
56+
return parseInt(rank);
57+
}
2658
}
2759

2860
// The line below allows us to load the getCardValue function into tests in other files.
@@ -41,12 +73,53 @@ function assertEquals(actualOutput, targetOutput) {
4173
// Examples:
4274
assertEquals(getCardValue("9♠"), 9);
4375

76+
// Test all ranks
77+
assertEquals(getCardValue("A♠"), 11);
78+
assertEquals(getCardValue("2♥"), 2);
79+
assertEquals(getCardValue("3♦"), 3);
80+
assertEquals(getCardValue("4♣"), 4);
81+
assertEquals(getCardValue("5♠"), 5);
82+
assertEquals(getCardValue("6♥"), 6);
83+
assertEquals(getCardValue("7♦"), 7);
84+
assertEquals(getCardValue("8♣"), 8);
85+
assertEquals(getCardValue("9♠"), 9);
86+
assertEquals(getCardValue("10♥"), 10);
87+
assertEquals(getCardValue("J♦"), 10);
88+
assertEquals(getCardValue("Q♣"), 10);
89+
assertEquals(getCardValue("K♠"), 10);
90+
91+
// Test different suits
92+
assertEquals(getCardValue("A♥"), 11);
93+
assertEquals(getCardValue("K♦"), 10);
94+
4495
// Handling invalid cards
4596
try {
4697
getCardValue("invalid");
47-
48-
// This line will not be reached if an error is thrown as expected
4998
console.error("Error was not thrown for invalid card");
50-
} catch (e) {}
99+
} catch (e) { }
100+
101+
// Test invalid rank
102+
try {
103+
getCardValue("1♠");
104+
console.error("Error was not thrown for invalid rank");
105+
} catch (e) { }
106+
107+
// Test invalid suit
108+
try {
109+
getCardValue("A♤");
110+
console.error("Error was not thrown for invalid suit");
111+
} catch (e) { }
112+
113+
// Test wrong length
114+
try {
115+
getCardValue("A");
116+
console.error("Error was not thrown for wrong length");
117+
} catch (e) { }
118+
119+
// Test non-string
120+
try {
121+
getCardValue(123);
122+
console.error("Error was not thrown for non-string");
123+
} catch (e) { }
51124

52125
// What other invalid card cases can you think of?

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(91)).toEqual("Obtuse angle");
24+
expect(getAngleType(135)).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(181)).toEqual("Reflex angle");
36+
expect(getAngleType(270)).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 <= 0 or >= 360`, () => {
42+
expect(getAngleType(0)).toEqual("Invalid angle");
43+
expect(getAngleType(-1)).toEqual("Invalid angle");
44+
expect(getAngleType(360)).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: 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+
// Positive proper fractions
13+
test(`should return true for positive proper fractions`, () => {
14+
expect(isProperFraction(1, 2)).toEqual(true);
15+
expect(isProperFraction(2, 3)).toEqual(true);
16+
expect(isProperFraction(7, 10)).toEqual(true);
17+
});
18+
19+
// Positive improper fractions
20+
test(`should return false for positive improper fractions`, () => {
21+
expect(isProperFraction(3, 2)).toEqual(false);
22+
expect(isProperFraction(5, 5)).toEqual(false);
23+
expect(isProperFraction(10, 3)).toEqual(false);
24+
});
25+
26+
// Negative numerator
27+
test(`should return false when numerator is negative`, () => {
28+
expect(isProperFraction(-1, 2)).toEqual(false);
29+
expect(isProperFraction(-3, 4)).toEqual(false);
30+
});
31+
32+
// Negative denominator
33+
test(`should return false when denominator is negative`, () => {
34+
expect(isProperFraction(1, -2)).toEqual(false);
35+
expect(isProperFraction(3, -4)).toEqual(false);
36+
});
37+
38+
// Zero numerator
39+
test(`should return false when numerator is zero`, () => {
40+
expect(isProperFraction(0, 2)).toEqual(false);
41+
expect(isProperFraction(0, -3)).toEqual(false);
42+
});
43+
44+
// Both negative (improper)
45+
test(`should return false for negative improper fractions`, () => {
46+
expect(isProperFraction(-3, -2)).toEqual(false);
47+
});
48+
49+
// Both negative (proper)
50+
test(`should return false for negative proper fractions`, () => {
51+
expect(isProperFraction(-1, -2)).toEqual(false);
52+
});

0 commit comments

Comments
 (0)