Skip to content

Commit 1c5754b

Browse files
committed
coursework/sprint-3-implement-and-rewrite1 clean
1 parent 8f3d6cf commit 1c5754b

File tree

6 files changed

+166
-50
lines changed

6 files changed

+166
-50
lines changed

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,24 @@ function getAngleType(angle) {
1111
if (angle === 90) {
1212
return "Right angle";
1313
}
14-
// Run the tests, work out what Case 2 is testing, and implement the required code here.
15-
// Then keep going for the other cases, one at a time.
14+
// Case 2: Identify Acute Angles
15+
else if (angle < 90) {
16+
return "Acute angle";
17+
}
18+
// Case 3: Identify Obtuse Angles
19+
else if (angle > 90 && angle < 180) {
20+
return "Obtuse angle";
21+
}
22+
// Case 4: Identify Straight Angles
23+
else if (angle === 180) {
24+
return "Straight angle";
25+
}
26+
// Case 5: Identify Reflex Angles
27+
else if (angle > 180 && angle < 360) {
28+
return "Reflex angle";
29+
} else {
30+
return "Invalid angle";
31+
}
1632
}
1733

1834
// The line below allows us to load the getAngleType function into tests in other files.
@@ -50,14 +66,18 @@ assertEquals(acute, "Acute angle");
5066
// When the angle is greater than 90 degrees and less than 180 degrees,
5167
// Then the function should return "Obtuse angle"
5268
const obtuse = getAngleType(120);
53-
// ====> write your test here, and then add a line to pass the test in the function above
54-
69+
assertEquals(obtuse, "Obtuse angle");
70+
//
5571
// Case 4: Identify Straight Angles:
5672
// When the angle is exactly 180 degrees,
5773
// Then the function should return "Straight angle"
58-
// ====> write your test here, and then add a line to pass the test in the function above
74+
const straight = getAngleType(180);
75+
assertEquals(straight, "Straight angle");
5976

6077
// Case 5: Identify Reflex Angles:
6178
// When the angle is greater than 180 degrees and less than 360 degrees,
6279
// Then the function should return "Reflex angle"
63-
// ====> write your test here, and then add a line to pass the test in the function above
80+
const reflex = getAngleType(270);
81+
assertEquals(reflex, "Reflex angle");
82+
83+
console.log("All tests completed");

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
// write one test at a time, and make it pass, build your solution up methodically
99

1010
function isProperFraction(numerator, denominator) {
11-
if (numerator < denominator) {
11+
// Case 1 & general case: numerator < denominator
12+
if (Math.abs(numerator) < denominator) {
1213
return true;
14+
} else {
15+
return false;
1316
}
1417
}
1518

@@ -26,7 +29,7 @@ function assertEquals(actualOutput, targetOutput) {
2629
}
2730

2831
// Acceptance criteria:
29-
32+
//
3033
// Proper Fraction check:
3134
// Input: numerator = 2, denominator = 3
3235
// target output: true
@@ -46,14 +49,22 @@ assertEquals(improperFraction, false);
4649
// target output: true
4750
// Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true.
4851
const negativeFraction = isProperFraction(-4, 7);
49-
// ====> complete with your assertion
52+
assertEquals(negativeFraction, true);
5053

5154
// Equal Numerator and Denominator check:
5255
// Input: numerator = 3, denominator = 3
5356
// target output: false
5457
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false.
5558
const equalFraction = isProperFraction(3, 3);
56-
// ====> complete with your assertion
59+
assertEquals(equalFraction, false);
60+
61+
// Stretch: other scenarios
62+
// Zero numerator check (0/x should always be proper if x != 0)
63+
const zeroNumerator = isProperFraction(0, 5);
64+
assertEquals(zeroNumerator, true);
65+
66+
// Negative denominator check (-2/5 is proper)
67+
const negativeDenominator = isProperFraction(2, -5);
68+
assertEquals(negativeDenominator, true);
5769

58-
// Stretch:
59-
// What other scenarios could you test for?
70+
console.log("All tests completed");

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

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,29 @@
77
// complete the rest of the tests and cases
88
// write one test at a time, and make it pass, build your solution up methodically
99
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
10+
1011
function getCardValue(card) {
12+
// Extract rank by removing the last character (the suit)
13+
const rank = card.slice(0, -1);
14+
15+
// Handle Ace
1116
if (rank === "A") {
1217
return 11;
1318
}
19+
20+
// Handle face cards
21+
if (["J", "Q", "K"].includes(rank) || rank === "10") {
22+
return 10;
23+
}
24+
25+
// Handle number cards 2-9
26+
const num = Number(rank);
27+
if (num >= 2 && num <= 9) {
28+
return num;
29+
}
30+
31+
// Invalid card
32+
throw new Error("Invalid card rank.");
1433
}
1534

1635
// The line below allows us to load the getCardValue function into tests in other files.
@@ -26,32 +45,49 @@ function assertEquals(actualOutput, targetOutput) {
2645
`Expected ${actualOutput} to equal ${targetOutput}`
2746
);
2847
}
48+
2949
// Acceptance criteria:
3050

51+
// Case 1: Ace card
3152
// Given a card string in the format "A♠" (representing a card in blackjack - the last character will always be an emoji for a suit, and all characters before will be a number 2-10, or one letter of J, Q, K, A),
3253
// When the function getCardValue is called with this card string as input,
3354
// Then it should return the numerical card value
3455
const aceofSpades = getCardValue("A♠");
3556
assertEquals(aceofSpades, 11);
3657

37-
// Handle Number Cards (2-10):
58+
// Case 2: Number Cards (2-10)
3859
// Given a card with a rank between "2" and "9",
3960
// When the function is called with such a card,
40-
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
61+
// Then it should return the numeric value corresponding to the rank
4162
const fiveofHearts = getCardValue("5♥");
42-
// ====> write your test here, and then add a line to pass the test in the function above
63+
assertEquals(fiveofHearts, 5);
64+
65+
const tenofDiamonds = getCardValue("10♦");
66+
assertEquals(tenofDiamonds, 10);
4367

44-
// Handle Face Cards (J, Q, K):
45-
// Given a card with a rank of "10," "J," "Q," or "K",
68+
// Case 3: Face Cards (J, Q, K)
69+
// Given a card with a rank of "J," "Q," or "K",
4670
// When the function is called with such a card,
47-
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
71+
// Then it should return 10
72+
//
73+
const jackofClubs = getCardValue("J♣");
74+
assertEquals(jackofClubs, 10);
4875

49-
// Handle Ace (A):
50-
// Given a card with a rank of "A",
51-
// When the function is called with an Ace,
52-
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
76+
const queenofHearts = getCardValue("Q♥");
77+
assertEquals(queenofHearts, 10);
5378

54-
// Handle Invalid Cards:
79+
const kingofSpades = getCardValue("K♠");
80+
assertEquals(kingofSpades, 10);
81+
82+
// Case 4: Invalid Cards
5583
// Given a card with an invalid rank (neither a number nor a recognized face card),
5684
// When the function is called with such a card,
57-
// Then it should throw an error indicating "Invalid card rank."
85+
// Then it should throw an error
86+
try {
87+
getCardValue("X♠");
88+
console.error("Expected error for invalid card rank");
89+
} catch (error) {
90+
assertEquals(error.message, "Invalid card rank.");
91+
}
92+
93+
console.log("All tests completed");
Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
// This statement loads the getAngleType function you wrote in the implement directory.
2-
// We will use the same function, but write tests for it using Jest in this file.
32
const getAngleType = require("../implement/1-get-angle-type");
43

4+
// Case 1: Identify Right Angles
55
test("should identify right angle (90°)", () => {
66
expect(getAngleType(90)).toEqual("Right angle");
77
});
88

9-
// REPLACE the comments with the tests
10-
// make your test descriptions as clear and readable as possible
11-
12-
// Case 2: Identify Acute Angles:
13-
// When the angle is less than 90 degrees,
14-
// Then the function should return "Acute angle"
9+
// Case 2: Identify Acute Angles
10+
//
11+
// When the angle is less than 90 degrees, it should return "Acute angle"
12+
test("should identify acute angle (<90°)", () => {
13+
expect(getAngleType(45)).toEqual("Acute angle");
14+
});
1515

16-
// Case 3: Identify Obtuse Angles:
17-
// When the angle is greater than 90 degrees and less than 180 degrees,
18-
// Then the function should return "Obtuse angle"
16+
// Case 3: Identify Obtuse Angles
17+
// When the angle is greater than 90 degrees and less than 180 degrees, it should return "Obtuse angle"
18+
test("should identify obtuse angle (>90° and <180°)", () => {
19+
expect(getAngleType(120)).toEqual("Obtuse angle");
20+
});
1921

20-
// Case 4: Identify Straight Angles:
21-
// When the angle is exactly 180 degrees,
22-
// Then the function should return "Straight angle"
22+
// Case 4: Identify Straight Angles
23+
// When the angle is exactly 180 degrees, it should return "Straight angle"
24+
test("should identify straight angle (180°)", () => {
25+
expect(getAngleType(180)).toEqual("Straight angle");
26+
});
2327

24-
// Case 5: Identify Reflex Angles:
25-
// When the angle is greater than 180 degrees and less than 360 degrees,
26-
// Then the function should return "Reflex angle"
28+
// Case 5: Identify Reflex Angles
29+
// When the angle is greater than 180 degrees and less than 360 degrees, it should return "Reflex angle"
30+
test("should identify reflex angle (>180° and <360°)", () => {
31+
expect(getAngleType(270)).toEqual("Reflex angle");
32+
});
Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
1-
// This statement loads the isProperFraction function you wrote in the implement directory.
2-
// We will use the same function, but write tests for it using Jest in this file.
31
const isProperFraction = require("../implement/2-is-proper-fraction");
42

5-
test("should return true for a proper fraction", () => {
3+
// Case 1: Proper fraction
4+
test("should return true for a proper fraction (numerator < denominator)", () => {
65
expect(isProperFraction(2, 3)).toEqual(true);
76
});
7+
//
88

9-
// Case 2: Identify Improper Fractions:
9+
// Case 2: Improper fraction
10+
test("should return false for an improper fraction (numerator > denominator)", () => {
11+
expect(isProperFraction(5, 2)).toEqual(false);
12+
});
13+
14+
// Case 3: Negative fraction
15+
test("should return true for a negative proper fraction (numerator < denominator)", () => {
16+
expect(isProperFraction(-4, 7)).toEqual(true);
17+
});
1018

11-
// Case 3: Identify Negative Fractions:
19+
// Case 4: Numerator equal to denominator
20+
test("should return false when numerator equals denominator", () => {
21+
expect(isProperFraction(3, 3)).toEqual(false);
22+
});
1223

13-
// Case 4: Identify Equal Numerator and Denominator:
24+
// Optional: Zero numerator
25+
test("should return true when numerator is zero", () => {
26+
expect(isProperFraction(0, 5)).toEqual(true);
27+
});
28+
29+
// Case 5: Negative denominator
30+
test("should return true when denominator is negative and proper", () => {
31+
expect(isProperFraction(2, -5)).toEqual(true);
32+
});

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,36 @@
22
// We will use the same function, but write tests for it using Jest in this file.
33
const getCardValue = require("../implement/3-get-card-value");
44

5+
// Case 1: Handle Ace
56
test("should return 11 for Ace of Spades", () => {
67
const aceofSpades = getCardValue("A♠");
78
expect(aceofSpades).toEqual(11);
89
});
910

10-
// Case 2: Handle Number Cards (2-10):
11-
// Case 3: Handle Face Cards (J, Q, K):
12-
// Case 4: Handle Ace (A):
13-
// Case 5: Handle Invalid Cards:
11+
// Case 2: Handle Number Cards (2-10)
12+
// When the card is "2" to "10", it should return the numeric value
13+
test("should return the correct value for number cards", () => {
14+
expect(getCardValue("5♥")).toEqual(5);
15+
expect(getCardValue("2♦")).toEqual(2);
16+
expect(getCardValue("10♣")).toEqual(10);
17+
});
18+
19+
// Case 3: Handle Face Cards (J, Q, K)
20+
// When the card is "J", "Q", or "K", it should return 10
21+
test("should return 10 for face cards J, Q, K", () => {
22+
expect(getCardValue("J♣")).toEqual(10);
23+
expect(getCardValue("Q♥")).toEqual(10);
24+
expect(getCardValue("K♠")).toEqual(10);
25+
});
26+
27+
// Case 4: Handle Ace (already handled in Case 1, but can check another suit)
28+
test("should return 11 for Ace of Hearts", () => {
29+
expect(getCardValue("A♥")).toEqual(11);
30+
});
31+
//
32+
// Case 5: Handle Invalid Cards
33+
// If the card rank is invalid, it should throw an error
34+
test("should throw error for invalid cards", () => {
35+
expect(() => getCardValue("X♠")).toThrow("Invalid card rank.");
36+
expect(() => getCardValue("11♣")).toThrow("Invalid card rank.");
37+
});

0 commit comments

Comments
 (0)