Skip to content

Commit 5845874

Browse files
sprint 3 coursework
1 parent 4c2c13b commit 5845874

File tree

11 files changed

+4645
-25
lines changed

11 files changed

+4645
-25
lines changed

Sprint-3/1-key-implement/1-get-angle-type.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
function getAngleType(angle) {
1111
if (angle === 90) return "Right angle";
12+
if (angle < 90) return "Acute angle";
13+
if (angle > 90 && angle < 180) return "Obtuse angle";
14+
if (angle === 180) return "Straight angle";
15+
if (angle > 180 && angle < 360) return "Reflex angle";
1216
// read to the end, complete line 36, then pass your test here
1317
}
1418

@@ -43,14 +47,19 @@ assertEquals(acute, "Acute angle");
4347
// When the angle is greater than 90 degrees and less than 180 degrees,
4448
// Then the function should return "Obtuse angle"
4549
const obtuse = getAngleType(120);
50+
assertEquals(obtuse, "Obtuse angle");
4651
// ====> write your test here, and then add a line to pass the test in the function above
4752

4853
// Case 4: Identify Straight Angles:
4954
// When the angle is exactly 180 degrees,
5055
// Then the function should return "Straight angle"
56+
const straight = getAngleType(180);
57+
assertEquals(straight, "Straight angle");
5158
// ====> write your test here, and then add a line to pass the test in the function above
5259

5360
// Case 5: Identify Reflex Angles:
5461
// When the angle is greater than 180 degrees and less than 360 degrees,
5562
// Then the function should return "Reflex angle"
63+
const reflex = getAngleType(270);
64+
assertEquals(reflex, "Reflex angle");
5665
// ====> write your test here, and then add a line to pass the test in the function above

Sprint-3/1-key-implement/2-is-proper-fraction.js

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +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) return true;
11+
// A proper fraction has a non-zero denominator,
12+
// and the absolute value of the numerator is less than the denominator.
13+
if (denominator === 0) return false;
14+
return Math.abs(numerator) < Math.abs(denominator);
15+
1216
}
1317

1418
// here's our helper again
@@ -24,30 +28,61 @@ function assertEquals(actualOutput, targetOutput) {
2428
// Proper Fraction check:
2529
// Input: numerator = 2, denominator = 3
2630
// target output: true
27-
// Explanation: The fraction 2/3 is a proper fraction, where the numerator is less than the denominator. The function should return true.
31+
// Explanation: The fraction 2/3 is a proper fraction, where the numerator
32+
// is less than the denominator. The function should return true.
2833
const properFraction = isProperFraction(2, 3);
2934
assertEquals(properFraction, true);
3035

3136
// Improper Fraction check:
3237
// Input: numerator = 5, denominator = 2
3338
// target output: false
34-
// Explanation: The fraction 5/2 is an improper fraction, where the numerator is greater than or equal to the denominator. The function should return false.
39+
// Explanation: The fraction 5/2 is an improper fraction, where the numerator
40+
// is greater than or equal to the denominator. The function should return false.
3541
const improperFraction = isProperFraction(5, 2);
3642
assertEquals(improperFraction, false);
3743

3844
// Negative Fraction check:
3945
// Input: numerator = -4, denominator = 7
4046
// target output: true
41-
// 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.
47+
// Explanation: The fraction -4/7 is a proper fraction because the absolute
48+
// value of the numerator (4) is less than the denominator (7).
49+
// The function should return true.
4250
const negativeFraction = isProperFraction(-4, 7);
51+
assertEquals(negativeFraction, true);
4352
// ====> complete with your assertion
4453

4554
// Equal Numerator and Denominator check:
4655
// Input: numerator = 3, denominator = 3
4756
// target output: false
48-
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false.
57+
// Explanation: The fraction 3/3 is not a proper fraction because the
58+
// numerator is equal to the denominator. The function should return false.
4959
const equalFraction = isProperFraction(3, 3);
60+
assertEquals(equalFraction, false);
5061
// ====> complete with your assertion
5162

5263
// Stretch:
5364
// What other scenarios could you test for?
65+
// Zero Numerator check:
66+
// Input: numerator = 0, denominator = 5
67+
// target output: true
68+
// Explanation: The fraction 0/5 is a proper fraction because the numerator
69+
// is less than the denominator. The function should return true.
70+
const zeroNumerator = isProperFraction(0, 5);
71+
assertEquals(zeroNumerator, true);
72+
73+
// Zero Denominator check:
74+
// Input: numerator = 3, denominator = 0
75+
// target output: false
76+
// Explanation: A fraction with a zero denominator is undefined,
77+
// so the function should return false.
78+
const zeroDenominator = isProperFraction(3, 0);
79+
assertEquals(zeroDenominator, false);
80+
81+
// Negative Denominator check:
82+
// Input: numerator = 3, denominator = -5
83+
// target output: true
84+
// Explanation: The fraction 3/-5 is a proper fraction because the absolute
85+
// value of the numerator is less than the absolute value of the denominator.
86+
// The function should return true.
87+
const negativeDenominator = isProperFraction(3, -5);
88+
assertEquals(negativeDenominator, true);

Sprint-3/1-key-implement/3-get-card-value.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,22 @@
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+
// Extract the rank from the card string
12+
const rank = card.slice(0, -1);
13+
// If the rank is "A", return 11
1114
if (rank === "A") return 11;
15+
16+
// If the rank is a number between "2" and "10", return its numeric value
17+
if (["2", "3", "4", "5", "6", "7", "8", "9", "10"].includes(rank)) {
18+
return parseInt(rank, 10); // Convert string to number
19+
}
20+
21+
// If the rank is "J", "Q", or "K", return 10
22+
if (["J", "Q", "K"].includes(rank)) return 10;
23+
24+
// If the rank is invalid, throw an error
25+
throw new Error("Invalid card rank");
26+
1227
}
1328

1429
// You need to write assertions for your function to check it works in different cases
@@ -33,19 +48,45 @@ assertEquals(aceofSpades, 11);
3348
// When the function is called with such a card,
3449
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
3550
const fiveofHearts = getCardValue("5♥");
51+
assertEquals(fiveofHearts, 5);
52+
53+
const twoOfClubs = getCardValue("2♣");
54+
assertEquals(twoOfClubs, 2);
55+
56+
const nineOfDiamonds = getCardValue("9♦");
57+
assertEquals(nineOfDiamonds, 9);
58+
3659
// ====> write your test here, and then add a line to pass the test in the function above
3760

3861
// Handle Face Cards (J, Q, K):
3962
// Given a card with a rank of "10," "J," "Q," or "K",
4063
// When the function is called with such a card,
4164
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
65+
const tenOfSpades = getCardValue("10♠");
66+
assertEquals(tenOfSpades, 10);
67+
68+
const jackOfDiamonds = getCardValue("J♦");
69+
assertEquals(jackOfDiamonds, 10);
70+
71+
const queenOfSpades = getCardValue("Q♠");
72+
assertEquals(queenOfSpades, 10);
73+
74+
const kingOfClubs = getCardValue("K♣");
75+
assertEquals(kingOfClubs, 10);
4276

4377
// Handle Ace (A):
4478
// Given a card with a rank of "A",
4579
// When the function is called with an Ace,
4680
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
81+
const aceOfClubs = getCardValue("A♣");
82+
assertEquals(aceOfClubs, 11);
4783

4884
// Handle Invalid Cards:
4985
// Given a card with an invalid rank (neither a number nor a recognized face card),
5086
// When the function is called with such a card,
5187
// Then it should throw an error indicating "Invalid card rank."
88+
try {
89+
getCardValue("X♠"); // Invalid card
90+
} catch (error) {
91+
assertEquals(error.message, "Invalid card rank");
92+
}

Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,21 @@ test("should identify right angle (90°)", () => {
77
// REPLACE the comments with the tests
88
// make your test descriptions as clear and readable as possible
99

10-
// Case 2: Identify Acute Angles:
11-
// When the angle is less than 90 degrees,
12-
// Then the function should return "Acute angle"
10+
test("should identify acute angles (less than 90°)", () => {
11+
expect(getAngleType(45)).toEqual("Acute angle");
12+
expect(getAngleType(89)).toEqual("Acute angle");
13+
});
1314

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

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

22-
// Case 5: Identify Reflex Angles:
23-
// When the angle is greater than 180 degrees and less than 360 degrees,
24-
// Then the function should return "Reflex angle"
24+
test("should identify reflex angles (greater than 180° and less than 360°)", () => {
25+
expect(getAngleType(200)).toEqual("Reflex angle");
26+
expect(getAngleType(359)).toEqual("Reflex angle");
27+
});

Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@ test("should return true for a proper fraction", () => {
44
expect(isProperFraction(2, 3)).toEqual(true);
55
});
66

7-
// Case 2: Identify Improper Fractions:
7+
test("should return false for an improper fraction (numerator > denominator)", () => {
8+
expect(isProperFraction(5, 4)).toEqual(false);
9+
});
810

9-
// Case 3: Identify Negative Fractions:
11+
test("should return false for negative fractions", () => {
12+
expect(isProperFraction(-2, 3)).toEqual(false);
13+
expect(isProperFraction(2, -3)).toEqual(false);
14+
expect(isProperFraction(-2, -3)).toEqual(false);
15+
});
1016

11-
// Case 4: Identify Equal Numerator and Denominator:
17+
test("should return false when numerator equals denominator", () => {
18+
expect(isProperFraction(3, 3)).toEqual(false);
19+
});

Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,46 @@ test("should return 11 for Ace of Spades", () => {
66
});
77

88
// Case 2: Handle Number Cards (2-10):
9+
test("should return correct value for number cards (2-10)", () => {
10+
const twoOfClubs = getCardValue("2♣");
11+
expect(twoOfClubs).toEqual(2);
12+
13+
const fiveOfDiamonds = getCardValue("5♦");
14+
expect(fiveOfDiamonds).toEqual(5);
15+
16+
const tenOfHearts = getCardValue("10♥");
17+
expect(tenOfHearts).toEqual(10);
18+
});
19+
920
// Case 3: Handle Face Cards (J, Q, K):
21+
test("should return 10 for face cards (J, Q, K)", () => {
22+
const jackOfSpades = getCardValue("J♠");
23+
expect(jackOfSpades).toEqual(10);
24+
25+
const queenOfClubs = getCardValue("Q♣");
26+
expect(queenOfClubs).toEqual(10);
27+
28+
const kingOfDiamonds = getCardValue("K♦");
29+
expect(kingOfDiamonds).toEqual(10);
30+
});
31+
1032
// Case 4: Handle Ace (A):
33+
test("should return 11 for any Ace card", () => {
34+
const aceOfClubs = getCardValue("A♣");
35+
expect(aceOfClubs).toEqual(11);
36+
37+
const aceOfDiamonds = getCardValue("A♦");
38+
expect(aceOfDiamonds).toEqual(11);
39+
40+
const aceOfHearts = getCardValue("A♥");
41+
expect(aceOfHearts).toEqual(11);
42+
});
43+
1144
// Case 5: Handle Invalid Cards:
45+
test("should return null for invalid cards", () => {
46+
const invalidCard = getCardValue("1♠");
47+
expect(invalidCard).toBeNull();
48+
49+
const emptyCard = getCardValue("");
50+
expect(emptyCard).toBeNull();
51+
});

Sprint-3/3-mandatory-practice/implement/count.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,10 @@ test("should count multiple occurrences of a character", () => {
2222
// And a character char that does not exist within the case-sensitive str,
2323
// When the function is called with these inputs,
2424
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
25+
26+
test("should return 0 if character does not exist in string", () => {
27+
const str = "hello";
28+
const char = "z";
29+
const count = countChar(str, char);
30+
expect(count).toEqual(0);
31+
});

Sprint-3/3-mandatory-practice/implement/get-ordinal-number.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,51 @@ const getOrdinalNumber = require("./get-ordinal-number");
1111
test("should return '1st' for 1", () => {
1212
expect(getOrdinalNumber(1)).toEqual("1st");
1313
});
14+
15+
// Case 2: Identify the ordinal number for 2
16+
// When the number is 2,
17+
// Then the function should return "2nd"
18+
test("should return '2nd' for 2", () => {
19+
expect(getOrdinalNumber(2)).toEqual("2nd");
20+
});
21+
22+
// Case 3: Identify the ordinal number for 3
23+
// When the number is 3,
24+
// Then the function should return "3rd"
25+
test("should return '3rd' for 3", () => {
26+
expect(getOrdinalNumber(3)).toEqual("3rd");
27+
});
28+
29+
// Case 4: Identify the ordinal number for numbers between 4 to 20
30+
// including teens
31+
// When the number is between 4 to 20,
32+
// Then the function should return the appropriate ordinal number
33+
// with "th" at the end
34+
test("should return '13th' for 13", () => {
35+
expect(getOrdinalNumber(13)).toEqual("13th");
36+
});
37+
38+
test("should return '11th' for 11", () => {
39+
expect(getOrdinalNumber(11)).toEqual("11th");
40+
});
41+
42+
test("should return '7th' for 7", () => {
43+
expect(getOrdinalNumber(7)).toEqual("7th");
44+
});
45+
46+
// Case 5: Identify ordinal for numbers ending with 1, 2, or 3
47+
// excluding teens
48+
// When the number ends with 1, 2, or 3,
49+
// Then the function should return the appropriate ordinal number
50+
// with "st", "nd", or "rd" at the end
51+
test("should return '21st' for 21", () => {
52+
expect(getOrdinalNumber(21)).toEqual("21st");
53+
});
54+
55+
test("should return '22nd' for 22", () => {
56+
expect(getOrdinalNumber(22)).toEqual("22nd");
57+
});
58+
59+
test("should return '23rd' for 23", () => {
60+
expect(getOrdinalNumber(23)).toEqual("23rd");
61+
});

Sprint-3/3-mandatory-practice/implement/repeat.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,32 @@ test("should repeat the string count times", () => {
2121
// When the repeat function is called with these inputs,
2222
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
2323

24+
test("should return the original string when count is 1", () => {
25+
const str = "busy";
26+
const count = 1;
27+
const repeatedStr = repeat(str, count);
28+
expect(repeatedStr).toEqual("busy");
29+
});
30+
2431
// case: Handle Count of 0:
2532
// Given a target string str and a count equal to 0,
2633
// When the repeat function is called with these inputs,
2734
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
2835

36+
test("should return an empty string when count is 0", () => {
37+
const str = "you";
38+
const count = 0;
39+
const repeatedStr = repeat(str, count);
40+
expect(repeatedStr).toEqual("");
41+
});
42+
2943
// case: Negative Count:
3044
// Given a target string str and a negative integer count,
3145
// When the repeat function is called with these inputs,
3246
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
47+
48+
test("should throw an error when count is negative", () => {
49+
const str = "square";
50+
const count = -4;
51+
expect(() => repeat(str, count)).toThrow("Count must be a non-negative integer");
52+
});

0 commit comments

Comments
 (0)