Skip to content

Commit 04a8f07

Browse files
committed
completed implement and rewrite tests exercises
1 parent 3372770 commit 04a8f07

File tree

6 files changed

+157
-13
lines changed

6 files changed

+157
-13
lines changed

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,23 @@
1414
// After you have implemented the function, write tests to cover all the cases, and
1515
// execute the code to ensure all tests pass.
1616

17+
//Question:
18+
// What about 360 angle that is a valid angle and called complete angle should I include that as well?
1719
function getAngleType(angle) {
1820
// TODO: Implement this function
21+
if (angle <= 0 || angle >= 360) {
22+
return "Invalid angle";
23+
} else if (angle < 90) {
24+
return "Acute angle";
25+
} else if (angle === 90) {
26+
return "Right angle";
27+
} else if (angle < 180) {
28+
return "Obtuse angle";
29+
} else if (angle === 180) {
30+
return "Straight angle";
31+
} else if (angle < 360) {
32+
return "Reflex angle";
33+
}
1934
}
2035

2136
// The line below allows us to load the getAngleType function into tests in other files.
@@ -33,5 +48,27 @@ function assertEquals(actualOutput, targetOutput) {
3348

3449
// TODO: Write tests to cover all cases, including boundary and invalid cases.
3550
// Example: Identify Right Angles
36-
const right = getAngleType(90);
37-
assertEquals(right, "Right angle");
51+
let angle = getAngleType(0);
52+
assertEquals(angle, "Invalid angle");
53+
angle = getAngleType(-78);
54+
assertEquals(angle, "Invalid angle");
55+
angle = getAngleType(1);
56+
assertEquals(angle, "Acute angle");
57+
angle = getAngleType(90);
58+
assertEquals(angle, "Right angle");
59+
angle = getAngleType(91);
60+
assertEquals(angle, "Obtuse angle");
61+
angle = getAngleType(180);
62+
assertEquals(angle, "Straight angle");
63+
angle = getAngleType(181);
64+
assertEquals(angle, "Reflex angle");
65+
angle = getAngleType(360);
66+
assertEquals(angle, "Invalid angle");
67+
angle = getAngleType(45671);
68+
assertEquals(angle, "Invalid angle");
69+
angle = getAngleType(0.5);
70+
assertEquals(angle, "Acute angle");
71+
angle = getAngleType(90.0098);
72+
assertEquals(angle, "Obtuse angle");
73+
angle = getAngleType(-0.0001);
74+
assertEquals(angle, "Invalid angle");

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

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

1313
function isProperFraction(numerator, denominator) {
1414
// TODO: Implement this function
15+
if (Math.abs(numerator) < Math.abs(denominator)) return true;
16+
else return false;
1517
}
1618

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

3234
// Example: 1/2 is a proper fraction
3335
assertEquals(isProperFraction(1, 2), true);
36+
assertEquals(isProperFraction(2, 2), false);
37+
assertEquals(isProperFraction(3, 2), false);
38+
assertEquals(isProperFraction(0, 2), true);
39+
assertEquals(isProperFraction(-1, 2), true);
40+
assertEquals(isProperFraction(-1, -2), true);
41+
assertEquals(isProperFraction(1, -2), true);
42+
assertEquals(isProperFraction(0, -1), true);
43+
assertEquals(isProperFraction(100, -100), false);
44+
assertEquals(isProperFraction(-100, 100), false);

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

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

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
// Handling invalid cards
26+
try{
27+
const rank = card.slice(0,-1); //rank of the card is everything except the last character of card string
28+
const suit = card.slice(-1); // suit is the last character of the card string
29+
if((card.length != 2 && card.length !=3) || !isValidCard(rank, suit)){
30+
throw new Error("Invalid card");
31+
}
32+
if(rank === "J" || rank === "Q" || rank == "K")
33+
return 10
34+
else if(rank == "A")
35+
return 11;
36+
else
37+
return Number(rank);
38+
}
39+
catch(e){
40+
return e.message;
41+
}
42+
}
43+
44+
function isValidCard(rank,suit){
45+
const ranks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
46+
const suits = ["♠", "♥", "♦", "♣"];
47+
if(ranks.includes(rank) && suits.includes(suit))
48+
return true;
49+
else
50+
return false;
2651
}
2752

2853
// The line below allows us to load the getCardValue function into tests in other files.
@@ -41,12 +66,14 @@ function assertEquals(actualOutput, targetOutput) {
4166
// Examples:
4267
assertEquals(getCardValue("9♠"), 9);
4368

44-
// Handling invalid cards
45-
try {
46-
getCardValue("invalid");
47-
48-
// This line will not be reached if an error is thrown as expected
49-
console.error("Error was not thrown for invalid card");
50-
} catch (e) {}
51-
52-
// What other invalid card cases can you think of?
69+
assertEquals(getCardValue("10♠"), 10);
70+
assertEquals(getCardValue("Q♠"), 10);
71+
assertEquals(getCardValue("A♣"), 11);
72+
assertEquals(getCardValue("2♠"), 2);
73+
assertEquals(getCardValue("J♦"), 10);
74+
assertEquals(getCardValue("K♠"), 10);
75+
assertEquals(getCardValue("Invalid"), "Invalid card");
76+
assertEquals(getCardValue("1Q"), "Invalid card");
77+
assertEquals(getCardValue("-10♦"), "Invalid card");
78+
assertEquals(getCardValue("♦K"), "Invalid card");
79+
assertEquals(getCardValue("Q♦♦"), "Invalid card");

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,40 @@ 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+
// Test various acute angles, including boundary cases
19+
expect(getAngleType(90)).toEqual("Right angle");
20+
});
21+
1722
// Case 3: Obtuse angles
23+
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
24+
// Test various acute angles, including boundary cases
25+
expect(getAngleType(91)).toEqual("Obtuse angle");
26+
expect(getAngleType(112)).toEqual("Obtuse angle");
27+
expect(getAngleType(179)).toEqual("Obtuse angle");
28+
});
29+
1830
// Case 4: Straight angle
31+
test(`should return "Straight angle" when angle = 180`, () => {
32+
// Test various acute angles, including boundary cases
33+
expect(getAngleType(180)).toEqual("Straight angle");
34+
});
35+
1936
// Case 5: Reflex angles
37+
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
38+
// Test various acute angles, including boundary cases
39+
expect(getAngleType(181)).toEqual("Reflex angle");
40+
expect(getAngleType(223)).toEqual("Reflex angle");
41+
expect(getAngleType(359)).toEqual("Reflex angle");
42+
});
43+
2044
// Case 6: Invalid angles
45+
test(`should return "Invalid angle" when angle >= 360 or angle <= 0)`, () => {
46+
// Test various acute angles, including boundary cases
47+
expect(getAngleType(0)).toEqual("Invalid angle");
48+
expect(getAngleType(-1)).toEqual("Invalid angle");
49+
expect(getAngleType(-2292)).toEqual("Invalid angle");
50+
expect(getAngleType(360)).toEqual("Invalid angle");
51+
expect(getAngleType(365)).toEqual("Invalid angle");
52+
expect(getAngleType(383949)).toEqual("Invalid angle");
53+
});

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
@@ -7,4 +7,15 @@ 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, 1)).toEqual(true);
11+
expect(isProperFraction(0, -1)).toEqual(true);
12+
expect(isProperFraction(1, 1)).toEqual(false);
13+
expect(isProperFraction(1, 2)).toEqual(true);
14+
expect(isProperFraction(1, -2)).toEqual(true);
15+
expect(isProperFraction(-6, -2)).toEqual(false);
16+
expect(isProperFraction(-2, -6)).toEqual(true);
17+
expect(isProperFraction(-200, -0)).toEqual(false);
18+
expect(isProperFraction(-0, -1000)).toEqual(true);
19+
expect(isProperFraction(5, 3)).toEqual(false);
20+
expect(isProperFraction(-25, 3)).toEqual(false);
1021
});

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,32 @@ test(`Should return 11 when given an ace card`, () => {
99
expect(getCardValue("A♠")).toEqual(11);
1010
});
1111

12+
// Case 2: Number Cards (2-10)
13+
test(`Should return number when given a number`, () => {
14+
expect(getCardValue("2♠")).toEqual(2);
15+
expect(getCardValue("10♠")).toEqual(10);
16+
expect(getCardValue("5♠")).toEqual(5);
17+
});
18+
19+
// Case 3: Face Cards (J, Q, K)
20+
test(`Should return 10 when given a face card`, () => {
21+
expect(getCardValue("Q♠")).toEqual(10);
22+
expect(getCardValue("K♠")).toEqual(10);
23+
expect(getCardValue("J♠")).toEqual(10);
24+
});
25+
26+
// Case 4: Invalid Cards
27+
test(`Should return Invalid Card when given an invalid card`, () => {
28+
expect(getCardValue("Aas♠")).toEqual("Invalid card");
29+
expect(getCardValue("What")).toEqual("Invalid card");
30+
expect(getCardValue("Q10")).toEqual("Invalid card");
31+
expect(getCardValue("11")).toEqual("Invalid card");
32+
expect(getCardValue("♠11")).toEqual("Invalid card");
33+
expect(getCardValue("10*")).toEqual("Invalid card");
34+
expect(getCardValue("Q_")).toEqual("Invalid card");
35+
expect(getCardValue("A10")).toEqual("Invalid card");
36+
});
37+
1238
// Suggestion: Group the remaining test data into these categories:
1339
// Number Cards (2-10)
1440
// Face Cards (J, Q, K)
@@ -17,4 +43,3 @@ test(`Should return 11 when given an ace card`, () => {
1743
// To learn how to test whether a function throws an error as expected in Jest,
1844
// please refer to the Jest documentation:
1945
// https://jestjs.io/docs/expect#tothrowerror
20-

0 commit comments

Comments
 (0)