Skip to content

Commit e3b6258

Browse files
committed
implement and rewrite tests are done
1 parent c5d6da1 commit e3b6258

File tree

4 files changed

+109
-38
lines changed

4 files changed

+109
-38
lines changed

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

Lines changed: 25 additions & 5 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
69+
assertEquals(obtuse, "Obtuse angle");
5470

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: 16 additions & 5 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

@@ -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);
5760

58-
// Stretch:
59-
// What other scenarios could you test for?
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);
69+
70+
console.log("All tests completed");

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

Lines changed: 47 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,48 @@ 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+
const jackofClubs = getCardValue("J♣");
73+
assertEquals(jackofClubs, 10);
4874

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.
75+
const queenofHearts = getCardValue("Q♥");
76+
assertEquals(queenofHearts, 10);
5377

54-
// Handle Invalid Cards:
78+
const kingofSpades = getCardValue("K♠");
79+
assertEquals(kingofSpades, 10);
80+
81+
// Case 4: Invalid Cards
5582
// Given a card with an invalid rank (neither a number nor a recognized face card),
5683
// When the function is called with such a card,
57-
// Then it should throw an error indicating "Invalid card rank."
84+
// Then it should throw an error
85+
try {
86+
getCardValue("X♠");
87+
console.error("Expected error for invalid card rank");
88+
} catch (error) {
89+
assertEquals(error.message, "Invalid card rank.");
90+
}
91+
92+
console.log("All tests completed");
Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
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+
// When the angle is less than 90 degrees, it should return "Acute angle"
11+
test("should identify acute angle (<90°)", () => {
12+
expect(getAngleType(45)).toEqual("Acute angle");
13+
});
1514

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

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

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"
27+
// Case 5: Identify Reflex Angles
28+
// When the angle is greater than 180 degrees and less than 360 degrees, it should return "Reflex angle"
29+
test("should identify reflex angle (>180° and <360°)", () => {
30+
expect(getAngleType(270)).toEqual("Reflex angle");
31+
});

0 commit comments

Comments
 (0)