Skip to content

Commit 6a1888e

Browse files
committed
Completed the tasks of the coursework
1 parent 3372770 commit 6a1888e

File tree

6 files changed

+200
-4
lines changed

6 files changed

+200
-4
lines changed

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

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

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

2139
// The line below allows us to load the getAngleType function into tests in other files.
@@ -35,3 +53,39 @@ function assertEquals(actualOutput, targetOutput) {
3553
// Example: Identify Right Angles
3654
const right = getAngleType(90);
3755
assertEquals(right, "Right angle");
56+
57+
// Acute Angles
58+
let acute = getAngleType(1);
59+
assertEquals(acute, "Acute angle");
60+
acute = getAngleType(45);
61+
assertEquals(acute, "Acute angle");
62+
acute = getAngleType(89);
63+
assertEquals(acute, "Acute angle");
64+
65+
// Obtuse Angles
66+
let obtuse = getAngleType(91);
67+
assertEquals(obtuse, "Obtuse angle");
68+
obtuse = getAngleType(135);
69+
assertEquals(obtuse, "Obtuse angle");
70+
obtuse = getAngleType(179);
71+
assertEquals(obtuse, "Obtuse angle");
72+
73+
// Straight Line Angles
74+
let straight = getAngleType(180);
75+
assertEquals(straight, "Straight angle");
76+
77+
// Reflex Angles
78+
let reflex = getAngleType(181);
79+
assertEquals(reflex, "Reflex angle");
80+
reflex = getAngleType(287);
81+
assertEquals(reflex, "Reflex angle");
82+
reflex = getAngleType(359);
83+
assertEquals(reflex, "Reflex angle");
84+
85+
// Invalid Angles
86+
let invalid = getAngleType(0);
87+
assertEquals(invalid, "Invalid angle");
88+
invalid = getAngleType(360);
89+
assertEquals(invalid, "Invalid angle");
90+
invalid = getAngleType(-20);
91+
assertEquals(invalid, "Invalid angle");

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

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

1313
function isProperFraction(numerator, denominator) {
1414
// TODO: Implement this function
15+
if (denominator === 0) {
16+
return false;
17+
}
18+
return Math.abs(numerator) < Math.abs(denominator);
1519
}
1620

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

3236
// Example: 1/2 is a proper fraction
3337
assertEquals(isProperFraction(1, 2), true);
38+
assertEquals(isProperFraction(5, 1), false);
39+
assertEquals(isProperFraction(7, 7), false);
40+
41+
42+
//Negative fractions
43+
assertEquals(isProperFraction(-5, -2), false);
44+
assertEquals(isProperFraction(-1, -2), true);
45+
assertEquals(isProperFraction(-5, 3), false);
46+
assertEquals(isProperFraction(1, -2), true);
47+
assertEquals(isProperFraction(4, -3), false);
48+
49+
//0 in Numerator or denominator
50+
assertEquals(isProperFraction(0, -2), true);
51+
assertEquals(isProperFraction(-1, 0), false);
52+
assertEquals(isProperFraction(0, 0), false);
53+
54+
// With decimals
55+
assertEquals(isProperFraction(2.5, -3), true);
56+
assertEquals(isProperFraction(-3.4, 6.3), true);
57+
assertEquals(isProperFraction(-7.4, 3.3), false);
58+

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

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,26 @@
2323

2424
function getCardValue(card) {
2525
// TODO: Implement this function
26+
const cardSuit = card.slice(-1);
27+
const cardRank = card.slice(0, -1);
28+
29+
const validSuits = ["♠", "♥", "♦", "♣"];
30+
const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
31+
32+
if (!validSuits.includes(cardSuit) || !validRanks.includes(cardRank)) {
33+
throw new Error("Invalid card");
34+
}
35+
36+
switch (cardRank) {
37+
case "A":
38+
return 11;
39+
case "K":
40+
case "Q":
41+
case "J":
42+
return 10;
43+
default:
44+
return Number(cardRank);
45+
}
2646
}
2747

2848
// The line below allows us to load the getCardValue function into tests in other files.
@@ -40,13 +60,44 @@ function assertEquals(actualOutput, targetOutput) {
4060
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4161
// Examples:
4262
assertEquals(getCardValue("9♠"), 9);
63+
assertEquals(getCardValue("3♣"), 3);
64+
assertEquals(getCardValue("10♦"), 10);
65+
assertEquals(getCardValue("J♥"), 10);
66+
assertEquals(getCardValue("Q♠"), 10);
67+
assertEquals(getCardValue("K♦"), 10);
68+
assertEquals(getCardValue("A♣"), 11);
4369

4470
// Handling invalid cards
4571
try {
46-
getCardValue("invalid");
47-
48-
// This line will not be reached if an error is thrown as expected
72+
getCardValue("11♠"); // This line will not be reached if an error is thrown as expected
73+
console.error("Error was not thrown for invalid card");
74+
} catch (e) {}
75+
try {
76+
getCardValue("01♦");
77+
console.error("Error was not thrown for invalid card");
78+
} catch (e) {}
79+
try {
80+
getCardValue("1♣");
81+
console.error("Error was not thrown for invalid card");
82+
} catch (e) {}
83+
try {
84+
getCardValue("");
85+
console.error("Error was not thrown for invalid card");
86+
} catch (e) {}
87+
try {
88+
getCardValue("K♠♠");
89+
console.error("Error was not thrown for invalid card");
90+
} catch (e) {}
91+
try {
92+
getCardValue("QQ♣");
93+
console.error("Error was not thrown for invalid card");
94+
} catch (e) {}
95+
try {
96+
getCardValue("♦");
97+
console.error("Error was not thrown for invalid card");
98+
} catch (e) {}
99+
try {
100+
getCardValue("J");
49101
console.error("Error was not thrown for invalid card");
50102
} catch (e) {}
51-
52103
// 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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,30 @@ 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 angle" when (90 < angle < 180)`, () => {
22+
expect(getAngleType(90.1)).toEqual("Obtuse angle");
23+
expect(getAngleType(135)).toEqual("Obtuse angle");
24+
expect(getAngleType(179.9)).toEqual("Obtuse angle");
25+
});
1826
// Case 4: Straight angle
27+
test(`should return "Straight angle" when (angle === 180)`, () => {
28+
expect(getAngleType(180)).toEqual("Straight angle");
29+
});
1930
// Case 5: Reflex angles
31+
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
32+
expect(getAngleType(180.1)).toEqual("Reflex angle");
33+
expect(getAngleType(271)).toEqual("Reflex angle");
34+
expect(getAngleType(359.9)).toEqual("Reflex angle");
35+
});
36+
2037
// Case 6: Invalid angles
38+
test(`should return "Invalid angle" when (angle >= 360 || angle === 0 || angle < 0)`, () => {
39+
expect(getAngleType(0)).toEqual("Invalid angle");
40+
expect(getAngleType(360)).toEqual("Invalid angle");
41+
expect(getAngleType(-25)).toEqual("Invalid angle");
42+
expect(getAngleType(460)).toEqual("Invalid angle");
43+
});

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,23 @@ 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, 0)).toEqual(false);
11+
});
12+
13+
// When numerator is greater than the denominator
14+
test(`should return false when numerator > denominator`, () => {
15+
expect(isProperFraction(5, 3)).toEqual(false);
16+
expect(isProperFraction(140, 7)).toEqual(false);
17+
expect(isProperFraction(-16, -5)).toEqual(false);
18+
expect(isProperFraction(-70, 14)).toEqual(false);
19+
expect(isProperFraction(100, -11)).toEqual(false);
20+
});
21+
22+
// When the fraction is correct
23+
test(`should return true when numerator < denominator`, () => {
24+
expect(isProperFraction(2, 5)).toEqual(true);
25+
expect(isProperFraction(1, -4)).toEqual(true);
26+
expect(isProperFraction(-100, -214)).toEqual(true);
27+
expect(isProperFraction(-1502, 4000)).toEqual(true);
28+
expect(isProperFraction(18, 32)).toEqual(true);
1029
});

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,36 @@ const getCardValue = require("../implement/3-get-card-value");
77
// Case 1: Ace (A)
88
test(`Should return 11 when given an ace card`, () => {
99
expect(getCardValue("A♠")).toEqual(11);
10+
expect(getCardValue("A♥")).toEqual(11);
11+
expect(getCardValue("A♣")).toEqual(11);
1012
});
1113

1214
// Suggestion: Group the remaining test data into these categories:
1315
// Number Cards (2-10)
16+
test(`Should return number when given an 2-10 card`, () => {
17+
expect(getCardValue("8♠")).toEqual(8);
18+
expect(getCardValue("5♥")).toEqual(5);
19+
expect(getCardValue("9♣")).toEqual(9);
20+
expect(getCardValue("2♥")).toEqual(2);
21+
expect(getCardValue("7♦")).toEqual(7);
22+
});
1423
// Face Cards (J, Q, K)
24+
test(`Should return 10 when given a face card`, () => {
25+
expect(getCardValue("J♠")).toEqual(10);
26+
expect(getCardValue("Q♣")).toEqual(10);
27+
expect(getCardValue("K♥")).toEqual(10);
28+
expect(getCardValue("J♦")).toEqual(10);
29+
});
30+
1531
// Invalid Cards
32+
test(`Should return Invalid when given an Invalid card`, () => {
33+
expect(() => getCardValue("11♠")).toThrow("Invalid card");
34+
expect(() => getCardValue("01♥")).toThrow("Invalid card");
35+
expect(() => getCardValue("1♣")).toThrow("Invalid card");
36+
expect(() => getCardValue("K♠♣")).toThrow("Invalid card");
37+
expect(() => getCardValue("QQ♥")).toThrow("Invalid card");
38+
expect(() => getCardValue("")).toThrow("Invalid card");
39+
});
1640

1741
// To learn how to test whether a function throws an error as expected in Jest,
1842
// please refer to the Jest documentation:

0 commit comments

Comments
 (0)