Skip to content

Commit b737af3

Browse files
committed
implement and rewrite sprint 3 pt 1
1 parent 3372770 commit b737af3

File tree

6 files changed

+165
-4
lines changed

6 files changed

+165
-4
lines changed

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

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

1717
function getAngleType(angle) {
18-
// TODO: Implement this function
18+
if (angle < 90) {
19+
return "Acute angle";
20+
}
21+
if (angle === 90) {
22+
return "Right angle";
23+
}
24+
if (angle < 180) {
25+
return "Obtuse angle";
26+
}
27+
if (angle === 180) {
28+
return "Straight angle";
29+
}
30+
if (angle <= 360) {
31+
return "Reflex angle";
32+
}
33+
return "Invalid angle";
1934
}
2035

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

3449
// TODO: Write tests to cover all cases, including boundary and invalid cases.
3550
// Example: Identify Right Angles
51+
const acute = getAngleType(70);
52+
assertEquals(acute, "Acute angle");
53+
3654
const right = getAngleType(90);
3755
assertEquals(right, "Right angle");
56+
57+
const obtuse = getAngleType(95);
58+
assertEquals(obtuse, "Obtuse angle");
59+
60+
const straight = getAngleType(180);
61+
assertEquals(straight, "Straight angle");
62+
63+
const reflex = getAngleType(280);
64+
assertEquals(reflex, "Reflex angle");
65+
66+
const invalid = getAngleType(180);
67+
assertEquals(invalid, "Invalid angle");

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

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

1313
function isProperFraction(numerator, denominator) {
14-
// TODO: Implement this function
14+
if (denominator === 0) return false;
15+
return numerator < denominator ? true : false;
1516
}
1617

1718
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -31,3 +32,8 @@ function assertEquals(actualOutput, targetOutput) {
3132

3233
// Example: 1/2 is a proper fraction
3334
assertEquals(isProperFraction(1, 2), true);
35+
assertEquals(isProperFraction(2, 1), false);
36+
assertEquals(isProperFraction(0, 0), false);
37+
assertEquals(isProperFraction(-0, 0), false);
38+
assertEquals(isProperFraction(-5, 4), true);
39+
assertEquals(isProperFraction(-4, -5), false);

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

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

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
const rank = [
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 suit = ["♠", "♥", "♦", "♣"];
41+
42+
const cardValue = card.slice(0, -1);
43+
console.log("card value: " + cardValue);
44+
const cardSuit = card[card.length - 1];
45+
46+
if (!rank.includes(cardValue) || !suit.includes(cardSuit)) {
47+
throw new Error("invalid input");
48+
}
49+
50+
if ("A" === cardValue) {
51+
return 11;
52+
}
53+
if (["J", "Q", "K"].includes(cardValue)) {
54+
return 10;
55+
}
56+
57+
return Number(cardValue);
2658
}
2759

2860
// The line below allows us to load the getCardValue function into tests in other files.
@@ -40,6 +72,13 @@ function assertEquals(actualOutput, targetOutput) {
4072
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4173
// Examples:
4274
assertEquals(getCardValue("9♠"), 9);
75+
assertEquals(getCardValue("A♠"), 11);
76+
assertEquals(getCardValue("J♠"), 10);
77+
assertEquals(getCardValue("K♠"), 10);
78+
assertEquals(getCardValue("Q♠"), 10);
79+
assertEquals(getCardValue("2♠"), 2);
80+
assertEquals(getCardValue("5♠"), 5);
81+
assertEquals(getCardValue("10♠"), 10);
4382

4483
// Handling invalid cards
4584
try {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,34 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
1111
expect(getAngleType(1)).toEqual("Acute angle");
1212
expect(getAngleType(45)).toEqual("Acute angle");
1313
expect(getAngleType(89)).toEqual("Acute angle");
14+
expect(getAngleType(95)).not.toEqual("Acute angle");
15+
});
16+
test(`should return "Right angle" when angle is 90)`, () => {
17+
// Test various acute angles, including boundary cases
18+
expect(getAngleType(90)).toEqual("Right angle");
19+
expect(getAngleType(45)).not.toEqual("Right angle");
20+
expect(getAngleType(99)).not.toEqual("Right angle");
21+
});
22+
23+
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
24+
// Test various acute angles, including boundary cases
25+
expect(getAngleType(90)).not.toEqual("Obtuse angle");
26+
expect(getAngleType(100)).toEqual("Obtuse angle");
27+
expect(getAngleType(180)).not.toEqual("Obtuse angle");
28+
expect(getAngleType(200)).not.toEqual("Obtuse angle");
29+
});
30+
31+
test(`should return "Straight angle" when (angle is 180)`, () => {
32+
// Test various acute angles, including boundary cases
33+
expect(getAngleType(179)).not.toEqual("Straight angle");
34+
expect(getAngleType(180)).toEqual("Straight angle");
35+
expect(getAngleType(181)).not.toEqual("Straight angle");
36+
});
37+
test(`should return "Reflex angle" when (180 < angle < 361)`, () => {
38+
// Test various acute angles, including boundary cases
39+
expect(getAngleType(179)).not.toEqual("Reflex angle");
40+
expect(getAngleType(371)).not.toEqual("Reflex angle");
41+
expect(getAngleType(200)).toEqual("Reflex angle");
1442
});
1543

1644
// Case 2: Right angle

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,42 @@ 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+
// This statement loads the isProperFraction function you wrote in the implement directory.
12+
// We will use the same function, but write tests for it using Jest in this file.
13+
const isProperFraction = require("../implement/2-is-proper-fraction");
14+
15+
// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
16+
17+
// Special case: numerator is zero
18+
test(`should return false when denominator is zero`, () => {
19+
expect(isProperFraction(1, 0)).toEqual(false);
20+
expect(isProperFraction(0, 0)).toEqual(false);
21+
expect(isProperFraction(-1, 0)).toEqual(false);
22+
});
23+
24+
test(`should return true when numerator and denominator both positive and numerator < denominator`, () => {
25+
expect(isProperFraction(1, 2)).toEqual(true);
26+
expect(isProperFraction(4, 5)).toEqual(true);
27+
expect(isProperFraction(2, 3)).toEqual(true);
28+
});
29+
30+
test(`should return true when numerator and denominator both negative and numerator < denominator`, () => {
31+
expect(isProperFraction(-3, -2)).toEqual(true);
32+
});
33+
34+
test(`should return true when numerator is negative and numerator < denominator`, () => {
35+
expect(isProperFraction(-3, 2)).toEqual(true);
36+
});
37+
38+
test(`should return false when numerator > denominator`, () => {
39+
expect(isProperFraction(2, 1)).toEqual(false);
40+
});
41+
42+
test(`should return false when numerator > denominator, negative nums`, () => {
43+
expect(isProperFraction(-1, -2)).toEqual(false);
44+
});
45+
46+
test(`should return false when numerator == denominator`, () => {
47+
expect(isProperFraction(-2, -2)).toEqual(false);
48+
expect(isProperFraction(2, 2)).toEqual(false);
49+
});

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,27 @@ test(`Should return 11 when given an ace card`, () => {
1313
// Number Cards (2-10)
1414
// Face Cards (J, Q, K)
1515
// Invalid Cards
16+
test(`Should return 10 when given a K, Q or J card`, () => {
17+
expect(getCardValue("K♠")).toEqual(10);
18+
expect(getCardValue("Q♠")).toEqual(10);
19+
expect(getCardValue("J♠")).toEqual(10);
20+
});
21+
22+
test(`Should return the number of the card given a numeric card card`, () => {
23+
expect(getCardValue("10♠")).toEqual(10);
24+
expect(getCardValue("2♠")).toEqual(2);
25+
expect(getCardValue("3♠")).toEqual(3);
26+
});
27+
28+
test(`Should throw an error if invalid card`, () => {
29+
expect(() => {
30+
getCardValue("2😬");
31+
}).toThrow();
32+
expect(() => {
33+
getCardValue("22♠");
34+
}).toThrow();
35+
});
1636

1737
// To learn how to test whether a function throws an error as expected in Jest,
1838
// please refer to the Jest documentation:
1939
// https://jestjs.io/docs/expect#tothrowerror
20-

0 commit comments

Comments
 (0)