Skip to content

Commit bf0a9bf

Browse files
authored
testing the code using assert
1 parent d873260 commit bf0a9bf

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@
1010
function getAngleType(angle) {
1111
if (angle === 90) {
1212
return "Right angle";
13+
}
14+
if (angle < 90) {
15+
return "Acute angle";
16+
}
17+
if (angle > 90 && angle < 180) {
18+
return "Obtuse angle";
19+
}
20+
if (angle === 180) {
21+
return "Straight angle";
22+
}
23+
if (angle > 180 && angle < 360) {
24+
return "Reflex angle";
1325
}
1426
// Run the tests, work out what Case 2 is testing, and implement the required code here.
1527
// Then keep going for the other cases, one at a time.
@@ -51,13 +63,17 @@ assertEquals(acute, "Acute angle");
5163
// Then the function should return "Obtuse angle"
5264
const obtuse = getAngleType(120);
5365
// ====> write your test here, and then add a line to pass the test in the function above
54-
66+
assertEquals(obtuse, "Obtuse angle");
5567
// Case 4: Identify Straight Angles:
5668
// When the angle is exactly 180 degrees,
5769
// Then the function should return "Straight angle"
5870
// ====> write your test here, and then add a line to pass the test in the function above
71+
const Straight = getAngleType(180);
72+
assertEquals(Straight, "Straight angle");
5973

6074
// Case 5: Identify Reflex Angles:
6175
// When the angle is greater than 180 degrees and less than 360 degrees,
6276
// 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
77+
// ====> write your test here, and then add a line to pass the test in the function above
78+
const reflex = getAngleType(220);
79+
assertEquals(reflex, "Reflex angle");

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ function isProperFraction(numerator, denominator) {
1111
if (numerator < denominator) {
1212
return true;
1313
}
14+
if (numerator > denominator) {
15+
return false;
16+
}
17+
if (numerator === denominator) {
18+
return true;
19+
}
20+
if (numerator === 0) {
21+
return true;
22+
}
23+
if (denominator===0) {
24+
return true;
25+
}
1426
}
1527

1628
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -47,13 +59,28 @@ assertEquals(improperFraction, false);
4759
// 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.
4860
const negativeFraction = isProperFraction(-4, 7);
4961
// ====> complete with your assertion
62+
assertEquals(negativeFraction, true);
5063

5164
// Equal Numerator and Denominator check:
5265
// Input: numerator = 3, denominator = 3
5366
// target output: false
5467
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false.
5568
const equalFraction = isProperFraction(3, 3);
5669
// ====> complete with your assertion
70+
assertEquals(equalFraction, true);
5771

5872
// Stretch:
5973
// What other scenarios could you test for?
74+
// zero value check:
75+
// Input: numerator = 0, denominator = 5
76+
// target output: true
77+
// Explanation: The fraction 0/5 will be zero because zero divided any number will give zero. The function should return true.
78+
const zeroValue = isProperFraction(0, 5);
79+
assertEquals(zeroValue, true);
80+
81+
// undefine value check:
82+
// Input: numerator = 5, denominator = 0
83+
// target output: true
84+
// Explanation: The fraction 5/0 will be undefine because any number divided by zero will be undefine. The function should return true.
85+
const undefine = isProperFraction(0, 5);
86+
assertEquals(undefine, true);

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,19 @@
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
1010
function getCardValue(card) {
11+
const rank = card.slice(0, -1);
1112
if (rank === "A") {
1213
return 11;
1314
}
15+
if (["K", "Q", "J"].includes(rank)) {
16+
return 10;
17+
}
18+
19+
const num = parseInt(rank, 10);
20+
if (num >= 2 && num <= 10) {
21+
return num;
22+
}
23+
throw new Error("Invalid card rank.");
1424
}
1525

1626
// The line below allows us to load the getCardValue function into tests in other files.
@@ -40,18 +50,39 @@ assertEquals(aceofSpades, 11);
4050
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4151
const fiveofHearts = getCardValue("5♥");
4252
// ====> write your test here, and then add a line to pass the test in the function above
53+
assertEquals(fiveofHearts, 5);
4354

4455
// Handle Face Cards (J, Q, K):
4556
// Given a card with a rank of "10," "J," "Q," or "K",
4657
// When the function is called with such a card,
4758
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
59+
const jack = getCardValue("J♦");
60+
assertEquals(jack, 10);
61+
62+
const queen = getCardValue("Q♣");
63+
assertEquals(queen, 10);
64+
65+
const king = getCardValue("K♠");
66+
assertEquals(king, 10);
67+
68+
const ten = getCardValue("10♥");
69+
assertEquals(ten, 10);
4870

4971
// Handle Ace (A):
5072
// Given a card with a rank of "A",
5173
// When the function is called with an Ace,
5274
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
75+
const Ace = getCardValue("A♦");
76+
assertEquals(Ace, 11);
5377

5478
// Handle Invalid Cards:
5579
// Given a card with an invalid rank (neither a number nor a recognized face card),
5680
// When the function is called with such a card,
5781
// Then it should throw an error indicating "Invalid card rank."
82+
try {
83+
getCardValue("B");
84+
console.error("Test failed: Expected error for invalid card rank.");
85+
} catch (e) {
86+
assertEquals(e.message, "Invalid card rank.");
87+
}
88+

0 commit comments

Comments
 (0)