Skip to content

Commit efaa7b0

Browse files
committed
implementing code and testing code
1 parent b31a586 commit efaa7b0

6 files changed

Lines changed: 185 additions & 6 deletions

File tree

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

Lines changed: 23 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 (0 < angle && angle < 90) {
19+
return "Acute angle";
20+
} else if (angle === 90) {
21+
return "Right angle";
22+
} else if (90 < angle && angle < 180) {
23+
return "Obtuse angle";
24+
} else if (angle === 180) {
25+
return "Straight angle";
26+
} else if (180 < angle && 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.
@@ -33,5 +45,15 @@ function assertEquals(actualOutput, targetOutput) {
3345

3446
// TODO: Write tests to cover all cases, including boundary and invalid cases.
3547
// Example: Identify Right Angles
48+
const Acute = getAngleType(0.5);
49+
assertEquals(Acute, "Acute angle");
3650
const right = getAngleType(90);
3751
assertEquals(right, "Right angle");
52+
const Obtuse = getAngleType(179);
53+
assertEquals(Obtuse, "Obtuse angle");
54+
const Straight = getAngleType(180);
55+
assertEquals(Straight, "Straight angle");
56+
const Reflex = getAngleType(181);
57+
assertEquals(Reflex, "Reflex angle");
58+
const Invalid = getAngleType(361);
59+
assertEquals(Invalid, "Invalid angle");

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

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

1313
function isProperFraction(numerator, denominator) {
14-
// TODO: Implement this function
14+
if (Math.abs(numerator) < Math.abs(denominator)) {
15+
return true;
16+
} else if (denominator === 0) {
17+
return false;
18+
} else {
19+
return false;
20+
}
1521
}
1622

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

3238
// Example: 1/2 is a proper fraction
3339
assertEquals(isProperFraction(1, 2), true);
40+
assertEquals(isProperFraction(0, 0.2), true);
41+
assertEquals(isProperFraction(2, 0), false);
42+
assertEquals(isProperFraction(11, 8), false);
43+
assertEquals(isProperFraction(0, 1), true);
44+
assertEquals(isProperFraction(10, 25), true);
45+
assertEquals(isProperFraction(0, 0), false);

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

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

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
const validRank = [
26+
"A",
27+
"2",
28+
"3",
29+
"4",
30+
"5",
31+
"6",
32+
"7",
33+
"8",
34+
"9",
35+
"10",
36+
"J",
37+
"Q",
38+
"K",
39+
];
40+
const validSuits = ["♠", "♣", "♦", "♥"];
41+
let rank = card.slice(0, -1);
42+
let suit = card.slice(-1);
43+
if (!validSuits.includes(suit)) {
44+
throw new Error("Invalid suit");
45+
}
46+
if (!validRank.includes(rank)) {
47+
throw new Error("Invalid rank");
48+
}
49+
if (rank === "A") {
50+
return 11;
51+
}
52+
if (["J", "Q", "K"].includes(rank)) {
53+
return 10;
54+
} else {
55+
return Number(rank);
56+
}
2657
}
27-
2858
// The line below allows us to load the getCardValue function into tests in other files.
2959
// This will be useful in the "rewrite tests with jest" step.
3060
module.exports = getCardValue;
@@ -40,7 +70,13 @@ function assertEquals(actualOutput, targetOutput) {
4070
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4171
// Examples:
4272
assertEquals(getCardValue("9♠"), 9);
43-
73+
assertEquals(getCardValue("A♠"), 11);
74+
assertEquals(getCardValue("J♣"), 10);
75+
assertEquals(getCardValue("K♦"), 10);
76+
assertEquals(getCardValue("Q♦"), 10);
77+
assertEquals(getCardValue("7♣"), 7);
78+
assertEquals(getCardValue("10♦"), 10);
79+
assertEquals(getCardValue("2♣"), 2);
4480
// Handling invalid cards
4581
try {
4682
getCardValue("invalid");
@@ -52,3 +88,51 @@ try {
5288
}
5389

5490
// What other invalid card cases can you think of?
91+
try {
92+
getCardValue("1♥");
93+
console.error("Error was not thrown for invalid card 😢");
94+
} catch (e) {
95+
console.log("Error thrown for invalid card 🎉");
96+
}
97+
try {
98+
getCardValue(" ");
99+
console.error("Error was not thrown for invalid card 😢");
100+
} catch (e) {
101+
console.log("Error thrown for invalid card 🎉");
102+
}
103+
try {
104+
getCardValue("Z♠");
105+
console.error("Error was not thrown for invalid card 😢");
106+
} catch (e) {
107+
console.log("Error thrown for invalid card 🎉");
108+
}
109+
try {
110+
getCardValue("G♠");
111+
console.error("Error was not thrown for invalid card 😢");
112+
} catch (e) {
113+
console.log("Error thrown for invalid card 🎉");
114+
}
115+
try {
116+
getCardValue("10X");
117+
console.error("Error was not thrown for invalid card 😢");
118+
} catch (e) {
119+
console.log("Error thrown for invalid card 🎉");
120+
}
121+
try {
122+
getCardValue("B♠");
123+
console.error("Error was not thrown for invalid card 😢");
124+
} catch (e) {
125+
console.log("Error thrown for invalid card 🎉");
126+
}
127+
try {
128+
getCardValue("♠");
129+
console.error("Error was not thrown for invalid card 😢");
130+
} catch (e) {
131+
console.log("Error thrown for invalid card 🎉");
132+
}
133+
try {
134+
getCardValue("B♠♠");
135+
console.error("Error was not thrown for invalid card 😢");
136+
} catch (e) {
137+
console.log("Error thrown for invalid card 🎉");
138+
}

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

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

1616
// Case 2: Right angle
17+
test(`should return "Right angle" when (angle === 90 )`, () => {
18+
expect(getAngleType(90)).toEqual("Right angle");
19+
});
1720
// Case 3: Obtuse angles
21+
test(`should return "Obtuse angles" when (90 < angle < 180)`, () => {
22+
expect(getAngleType(91)).toEqual("Obtuse angles");
23+
expect(getAngleType(179)).toEqual("Obtuse angles");
24+
expect(getAngleType(145)).toEqual("Obtuse angles");
25+
expect(getAngleType(92)).toEqual("Obtuse angles");
26+
expect(getAngleType(178)).toEqual("Obtuse angles");
27+
});
1828
// Case 4: Straight angle
29+
test(`should return "Straight angle" when (angle === 180)`, () => {
30+
expect(getAngleType(180)).toEqual("Straight angle");
31+
});
1932
// Case 5: Reflex angles
33+
test(`should return "Reflex angles" when (180 < angle < 360)`, () => {
34+
expect(getAngleType(181)).toEqual("Reflex angles");
35+
expect(getAngleType(190)).toEqual("Reflex angles");
36+
expect(getAngleType(345)).toEqual("Reflex angles");
37+
expect(getAngleType(359)).toEqual("Reflex angles");
38+
expect(getAngleType(277)).toEqual("Reflex angles");
39+
});
2040
// Case 6: Invalid angles
41+
test(`should return "Invalid angles" when (angle )`, () => {
42+
expect(getAngleType(-60)).toEqual("Invalid angles");
43+
expect(getAngleType(450)).toEqual("Invalid angles");
44+
expect(getAngleType(-2)).toEqual("Invalid angles");
45+
expect(getAngleType(361)).toEqual("Invalid angles");
46+
expect(getAngleType(890)).toEqual("Invalid angles");
47+
});

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
@@ -8,3 +8,14 @@ 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+
test(`should return false when the denominator less nominator`, () => {
12+
expect(isProperFraction(7, 2)).toEqual(false);
13+
expect(isProperFraction(-17, -2)).toEqual(false);
14+
expect(isProperFraction(0.5, 0.1)).toEqual(false);
15+
});
16+
test(`should return true when denominator is greater than nominator`, () => {
17+
expect(isProperFraction(2, 8)).toEqual(true);
18+
expect(isProperFraction(-3, -10)).toEqual(true);
19+
expect(isProperFraction(-3, 7)).toEqual(true);
20+
expect(isProperFraction(8, 17)).toEqual(true);
21+
});

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,30 @@ 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: Face Cards (J, Q, K)
12+
test(`Should return 10 when given a Face Cards`, () => {
13+
expect(getCardValue("J♣")).toEqual(10);
14+
expect(getCardValue("K♦")).toEqual(10);
15+
expect(getCardValue("Q♥")).toEqual(10);
16+
});
17+
//Case 3: Number Cards (2-10)
18+
test(`Should return the Number it self when a given Number card`, () => {
19+
expect(getCardValue("2♣")).toEqual(2);
20+
expect(getCardValue("10♦")).toEqual(10);
21+
expect(getCardValue("4♥")).toEqual(4);
22+
expect(getCardValue("6♣")).toEqual(6);
23+
expect(getCardValue("7♠")).toEqual(7);
24+
expect(getCardValue("9♥")).toEqual(9);
25+
});
26+
// Invalid Cards
27+
test(`Should return Invalid Cards when given Invalid Cards`, () => {
28+
expect(getCardValue("22♣")).toEqual("Invalid Cards");
29+
expect(getCardValue("11♦")).toEqual("Invalid Cards");
30+
expect(getCardValue("4♥♥")).toEqual("Invalid Cards");
31+
expect(getCardValue("")).toEqual("Invalid Cards");
32+
expect(getCardValue("♠")).toEqual("Invalid Cards");
33+
expect(getCardValue("-9♥")).toEqual("Invalid Cards");
34+
});
1135

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

0 commit comments

Comments
 (0)