Skip to content

Commit 98d1215

Browse files
committed
Sprint 3: implement solutions and rewrite tests with Jest
1 parent 083a9d2 commit 98d1215

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,26 @@ function getAngleType(angle) {
1313
}
1414
// Run the tests, work out what Case 2 is testing, and implement the required code here.
1515
// Then keep going for the other cases, one at a time.
16+
17+
if (angle < 90) {
18+
return "Acute angle";
19+
}
20+
21+
if (angle > 90 && angle < 180) {
22+
return "Obtuse angle";
23+
}
24+
25+
if (angle === 180) {
26+
return "Straight angle";
27+
}
28+
29+
if (angle > 180 && angle < 360) {
30+
return "Reflex angle";
31+
}
32+
1633
}
1734

35+
1836
// The line below allows us to load the getAngleType function into tests in other files.
1937
// This will be useful in the "rewrite tests with jest" step.
2038
module.exports = getAngleType;
@@ -49,15 +67,20 @@ assertEquals(acute, "Acute angle");
4967
// Case 3: Identify Obtuse Angles:
5068
// When the angle is greater than 90 degrees and less than 180 degrees,
5169
// Then the function should return "Obtuse angle"
52-
const obtuse = getAngleType(120);
5370
// ====> write your test here, and then add a line to pass the test in the function above
71+
const obtuse = getAngleType(120);
72+
assertEquals(obtuse, "Obtuse angle");
5473

5574
// Case 4: Identify Straight Angles:
5675
// When the angle is exactly 180 degrees,
5776
// Then the function should return "Straight angle"
5877
// ====> write your test here, and then add a line to pass the test in the function above
78+
const straight = getAngleType(180);
79+
assertEquals(straight, "Straight angle");
5980

6081
// Case 5: Identify Reflex Angles:
6182
// When the angle is greater than 180 degrees and less than 360 degrees,
6283
// 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
84+
// ====> write your test here, and then add a line to pass the test in the function above
85+
const reflex = getAngleType(270);
86+
assertEquals(reflex, "Reflex angle");

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
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) {
12-
return true;
13-
}
11+
return numerator < denominator;
1412
}
1513

1614
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -46,14 +44,15 @@ assertEquals(improperFraction, false);
4644
// target output: true
4745
// 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.
4846
const negativeFraction = isProperFraction(-4, 7);
49-
// ====> complete with your assertion
47+
assertEquals(negativeFraction, true);
48+
5049

5150
// Equal Numerator and Denominator check:
5251
// Input: numerator = 3, denominator = 3
5352
// target output: false
5453
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false.
5554
const equalFraction = isProperFraction(3, 3);
56-
// ====> complete with your assertion
55+
assertEquals(equalFraction, false);
5756

5857
// Stretch:
5958
// What other scenarios could you test for?

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@
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-
function getCardValue(card) {
10+
function getCardValue(card){
11+
const rank = card.slice(0, -1);
1112
if (rank === "A") {
1213
return 11;
1314
}
15+
if (rank === "10" || rank === "J" || rank === "Q" || rank === "K") {
16+
return 10;
17+
}
18+
19+
throw new Error ("Invalid card rank"); {
20+
1421
}
1522

23+
1624
// The line below allows us to load the getCardValue function into tests in other files.
1725
// This will be useful in the "rewrite tests with jest" step.
1826
module.exports = getCardValue;
@@ -39,19 +47,25 @@ assertEquals(aceofSpades, 11);
3947
// When the function is called with such a card,
4048
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4149
const fiveofHearts = getCardValue("5♥");
50+
assertEquals(fiveofHearts, 5);
4251
// ====> write your test here, and then add a line to pass the test in the function above
4352

4453
// Handle Face Cards (J, Q, K):
4554
// Given a card with a rank of "10," "J," "Q," or "K",
4655
// When the function is called with such a card,
4756
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
57+
const king = getCardValue("K♣");
58+
assertEquals(king, 10);
4859

4960
// Handle Ace (A):
5061
// Given a card with a rank of "A",
5162
// When the function is called with an Ace,
5263
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
53-
54-
// Handle Invalid Cards:
55-
// Given a card with an invalid rank (neither a number nor a recognized face card),
56-
// When the function is called with such a card,
57-
// Then it should throw an error indicating "Invalid card rank."
64+
const ace = getCardValue("A♠");
65+
assertEquals(ace, 11);
66+
try {
67+
getCardValue("Z♠");
68+
} catch (error) {
69+
assertEquals(error.message, "Invaild card rank");
70+
}
71+
}

0 commit comments

Comments
 (0)