Skip to content

Commit c0f5e98

Browse files
committed
Sprint 3 implement functions and tests
1 parent 3372770 commit c0f5e98

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed

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

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

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

2135
// The line below allows us to load the getAngleType function into tests in other files.
@@ -35,3 +49,29 @@ function assertEquals(actualOutput, targetOutput) {
3549
// Example: Identify Right Angles
3650
const right = getAngleType(90);
3751
assertEquals(right, "Right angle");
52+
53+
// Acute angle test
54+
const acute = getAngleType(45);
55+
assertEquals(acute, "Acute angle");
56+
57+
// Obtuse angle test
58+
const obtuse = getAngleType(120);
59+
assertEquals(obtuse, "Obtuse angle");
60+
61+
// Straight angle test
62+
const straight = getAngleType(180);
63+
assertEquals(straight, "Straight angle");
64+
65+
// Reflex angle test
66+
const reflex = getAngleType(270);
67+
assertEquals(reflex, "Reflex angle");
68+
69+
// Boundary tests
70+
assertEquals(getAngleType(1), "Acute angle");
71+
assertEquals(getAngleType(359), "Reflex angle");
72+
73+
// Invalid angle tests
74+
assertEquals(getAngleType(0), "Invalid angle");
75+
assertEquals(getAngleType(360), "Invalid angle");
76+
assertEquals(getAngleType(-10), "Invalid angle");
77+
assertEquals(getAngleType(500), "Invalid angle");

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

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

1313
function isProperFraction(numerator, denominator) {
1414
// TODO: Implement this function
15+
16+
if (denominator === 0) {
17+
return false;
18+
}
19+
20+
if (Math.abs(numerator) < Math.abs(denominator)) {
21+
return true;
22+
}
23+
24+
return false;
1525
}
1626

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

3242
// Example: 1/2 is a proper fraction
3343
assertEquals(isProperFraction(1, 2), true);
44+
45+
// numerator smaller than denominator
46+
assertEquals(isProperFraction(3, 4), true);
47+
48+
// numerator equal to denominator (not proper)
49+
assertEquals(isProperFraction(5, 5), false);
50+
51+
// numerator larger than denominator
52+
assertEquals(isProperFraction(7, 3), false);
53+
54+
// numerator is zero
55+
assertEquals(isProperFraction(0, 5), true);
56+
57+
// denominator is zero (invalid)
58+
assertEquals(isProperFraction(1, 0), false);
59+
60+
// negative numbers
61+
assertEquals(isProperFraction(-1, 2), true);
62+
assertEquals(isProperFraction(1, -2), true);
63+
assertEquals(isProperFraction(-3, -2), false);

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

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,30 @@
2323

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

2852
// The line below allows us to load the getCardValue function into tests in other files.
@@ -41,6 +65,18 @@ function assertEquals(actualOutput, targetOutput) {
4165
// Examples:
4266
assertEquals(getCardValue("9♠"), 9);
4367

68+
// Ace
69+
assertEquals(getCardValue("A♠"), 11);
70+
71+
// Face cards
72+
assertEquals(getCardValue("J♥"), 10);
73+
assertEquals(getCardValue("Q♦"), 10);
74+
assertEquals(getCardValue("K♣"), 10);
75+
76+
// Number cards
77+
assertEquals(getCardValue("2♠"), 2);
78+
assertEquals(getCardValue("10♥"), 10);
79+
4480
// Handling invalid cards
4581
try {
4682
getCardValue("invalid");
@@ -49,4 +85,15 @@ try {
4985
console.error("Error was not thrown for invalid card");
5086
} catch (e) {}
5187

52-
// What other invalid card cases can you think of?
88+
// More invalid cases
89+
try {
90+
getCardValue("11♠");
91+
console.error("Error was not thrown for invalid card");
92+
} catch (e) {}
93+
94+
try {
95+
getCardValue("A");
96+
console.error("Error was not thrown for invalid card");
97+
} catch (e) {}
98+
99+
// What other invalid card cases can you think of?

0 commit comments

Comments
 (0)