Skip to content

Commit fedc6d6

Browse files
committed
Implement getCardValue and add Jest tests
1 parent b31a586 commit fedc6d6

6 files changed

Lines changed: 172 additions & 5 deletions

File tree

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

Lines changed: 33 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,23 @@ function assertEquals(actualOutput, targetOutput) {
3547
// Example: Identify Right Angles
3648
const right = getAngleType(90);
3749
assertEquals(right, "Right angle");
50+
51+
// Identify Acute Angles
52+
const acute = getAngleType(45);
53+
assertEquals(acute, "Acute angle");
54+
55+
// Identify Obtuse Angles
56+
const obtuse = getAngleType(120);
57+
assertEquals(obtuse, "Obtuse angle");
58+
59+
// Identify Straight Angles
60+
const straight = getAngleType(180);
61+
assertEquals(straight, "Straight angle");
62+
63+
// Identify Reflex Angles
64+
const reflex = getAngleType(270);
65+
assertEquals(reflex, "Reflex angle");
66+
67+
// Identify Invalid Angles
68+
const invalid = getAngleType(400);
69+
assertEquals(invalid, "Invalid angle");

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

Lines changed: 15 additions & 2 deletions
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 < denominator;
1515
}
1616

1717
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -29,5 +29,18 @@ function assertEquals(actualOutput, targetOutput) {
2929
// TODO: Write tests to cover all cases.
3030
// What combinations of numerators and denominators should you test?
3131

32-
// Example: 1/2 is a proper fraction
32+
// Example #1: 1/2 is a proper fraction
3333
assertEquals(isProperFraction(1, 2), true);
34+
35+
// Example #2: 1/0 is not a proper fraction
36+
assertEquals(isProperFraction(1, 0), false);
37+
38+
// Example #3: 5/5 is not a proper fraction
39+
assertEquals(isProperFraction(5, 5), false);
40+
41+
// Example #4: 7/4 is not a proper fraction
42+
assertEquals(isProperFraction(7, 4), false);
43+
44+
// Example #5: -1/2 and -5/-2 are proper fractions
45+
assertEquals(isProperFraction(-1, 2), true);
46+
assertEquals(isProperFraction(-5, -2), true);

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

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,31 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
const rank = card.slice(0, -1);
26+
const suit = card.slice(-1);
27+
28+
const values = {
29+
A: 11,
30+
J: 10,
31+
Q: 10,
32+
K: 10,
33+
};
34+
35+
const validSuits = ["♠", "♥", "♦", "♣"];
36+
37+
if (!validSuits.includes(suit)) {
38+
throw new Error("Invalid card");
39+
}
40+
41+
if (values[rank]) {
42+
return values[rank];
43+
}
44+
45+
if (Number(rank) >= 2 && Number(rank) <= 10) {
46+
return Number(rank);
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.
@@ -52,3 +76,38 @@ try {
5276
}
5377

5478
// What other invalid card cases can you think of?
79+
// Invalid rank
80+
try {
81+
getCardValue("1♠");
82+
83+
console.error("Error was not thrown for invalid rank 😢");
84+
} catch (e) {
85+
console.log("Error thrown for invalid rank 🎉");
86+
}
87+
88+
// Invalid suit
89+
try {
90+
getCardValue("9X");
91+
92+
console.error("Error was not thrown for invalid suit 😢");
93+
} catch (e) {
94+
console.log("Error thrown for invalid suit 🎉");
95+
}
96+
97+
// Missing suit
98+
try {
99+
getCardValue("A");
100+
101+
console.error("Error was not thrown for missing suit 😢");
102+
} catch (e) {
103+
console.log("Error thrown for missing suit 🎉");
104+
}
105+
106+
// Missing rank
107+
try {
108+
getCardValue("♠");
109+
110+
console.error("Error was not thrown for missing rank 😢");
111+
} catch (e) {
112+
console.log("Error thrown for missing rank 🎉");
113+
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,29 @@ 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 exactly 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(120)).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 exactly 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(270)).toEqual("Reflex angle");
36+
});
37+
2038
// Case 6: Invalid angles
39+
test(`should return "Invalid angle" for invalid angles`, () => {
40+
expect(getAngleType(0)).toEqual("Invalid angle");
41+
expect(getAngleType(360)).toEqual("Invalid angle");
42+
});

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,28 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
44

55
// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
66

7-
// Special case: numerator is zero
7+
// Special case: denominator is zero
88
test(`should return false when denominator is zero`, () => {
99
expect(isProperFraction(1, 0)).toEqual(false);
1010
});
11+
12+
// Case #1: numerator is smaller than denominator
13+
test(`should return true when numerator is smaller than denominator`, () => {
14+
expect(isProperFraction(1, 2)).toEqual(true);
15+
});
16+
17+
// Case #2: numerator is equal to denominator
18+
test(`should return false when numerator and denominator are equal`, () => {
19+
expect(isProperFraction(5, 5)).toEqual(false);
20+
});
21+
22+
// Case #3: numerator is bigger than denominator
23+
test(`should return false when numerator is greater than denominator`, () => {
24+
expect(isProperFraction(7, 4)).toEqual(false);
25+
});
26+
27+
// Case #4: numerator and/or denominator are negative numbers
28+
test(`should handle negative numbers`, () => {
29+
expect(isProperFraction(-1, 2)).toEqual(true);
30+
expect(isProperFraction(-5, -2)).toEqual(true);
31+
});

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,23 @@ test(`Should return 11 when given an ace card`, () => {
1818
// please refer to the Jest documentation:
1919
// https://jestjs.io/docs/expect#tothrowerror
2020

21+
// Number Cards (2-10)
22+
test(`Should return the number value when given a number card`, () => {
23+
expect(getCardValue("2♥")).toEqual(2);
24+
expect(getCardValue("5♦")).toEqual(5);
25+
expect(getCardValue("10♣")).toEqual(10);
26+
});
27+
28+
// Face Cards (J, Q, K)
29+
test(`Should return 10 when given a face card`, () => {
30+
expect(getCardValue("J♠")).toEqual(10);
31+
expect(getCardValue("Q♥")).toEqual(10);
32+
expect(getCardValue("K♦")).toEqual(10);
33+
});
34+
35+
// Invalid Cards
36+
test(`Should throw an error when given an invalid card`, () => {
37+
expect(() => getCardValue("invalid")).toThrow();
38+
expect(() => getCardValue("1♠")).toThrow();
39+
expect(() => getCardValue("9X")).toThrow();
40+
});

0 commit comments

Comments
 (0)