Skip to content

Commit c572b4d

Browse files
Sprint-3 coursework
1 parent b5dc2cf commit c572b4d

19 files changed

+4356
-46
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
// Then, write the next test! :) Go through this process until all the cases are implemented
99

1010
function getAngleType(angle) {
11-
if (angle === 90) {
12-
return "Right angle";
13-
}
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.
11+
if (angle === 90) return "Right angle";
12+
else if (angle < 90) return "Acute angle";
13+
else if (angle > 90 && angle < 180) return "Obtuse angle";
14+
else if (angle === 180) return "Straight angle";
15+
else return "Reflex angle";
16+
// Run the tests, work out what Case 2 is testing, and implement the required code here.
17+
// Then keep going for the other cases, one at a time.
1618
}
1719

1820
// The line below allows us to load the getAngleType function into tests in other files.
@@ -50,14 +52,16 @@ assertEquals(acute, "Acute angle");
5052
// When the angle is greater than 90 degrees and less than 180 degrees,
5153
// Then the function should return "Obtuse angle"
5254
const obtuse = getAngleType(120);
53-
// ====> write your test here, and then add a line to pass the test in the function above
55+
assertEquals(obtuse, "Obtuse angle");
5456

5557
// Case 4: Identify Straight Angles:
5658
// When the angle is exactly 180 degrees,
5759
// 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
60+
const straight = getAngleType(180);
61+
assertEquals(straight, "Straight angle");
5962

6063
// Case 5: Identify Reflex Angles:
6164
// When the angle is greater than 180 degrees and less than 360 degrees,
6265
// 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
66+
const reflex = getAngleType(260);
67+
assertEquals(reflex, "Reflex angle");

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +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) {
12-
return true;
13-
}
11+
numerator = Math.abs(numerator);
12+
denominator = Math.abs(denominator);
13+
if (numerator < denominator) return true;
14+
else if (numerator > denominator) return false;
15+
else if (numerator === denominator) return false;
1416
}
1517

1618
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -46,14 +48,24 @@ assertEquals(improperFraction, false);
4648
// target output: true
4749
// 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.
4850
const negativeFraction = isProperFraction(-4, 7);
49-
// ====> complete with your assertion
51+
assertEquals(negativeFraction, true);
5052

5153
// Equal Numerator and Denominator check:
5254
// Input: numerator = 3, denominator = 3
5355
// target output: false
5456
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false.
5557
const equalFraction = isProperFraction(3, 3);
56-
// ====> complete with your assertion
58+
assertEquals(equalFraction, false);
5759

5860
// Stretch:
59-
// What other scenarios could you test for?
61+
// Input: numerator = 4, denominator = -7
62+
// target output: true
63+
// 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.
64+
const negativeFraction1 = isProperFraction(4, -7);
65+
assertEquals(negativeFraction1, true);
66+
67+
// Input: numerator = -4, denominator = -7
68+
// target output: true
69+
// 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.
70+
const negativeFraction2 = isProperFraction(4, 7);
71+
assertEquals(negativeFraction2, true);

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@
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-
if (rank === "A") {
12-
return 11;
13-
}
11+
if (!["♠", "♣", "♦", "♥"].includes(card.slice(-1)))
12+
throw new Error("Invalid card rank.");
13+
const rank = card.slice(0, card.length - 1);
14+
if (rank === "A") return 11;
15+
if (["10", "J", "Q", "K"].includes(rank)) return 10;
16+
if (["2", "3", "4", "5", "6", "7", "8", "9"].includes(rank))
17+
return Number(rank);
18+
throw new Error("Invalid card rank.");
1419
}
1520

1621
// The line below allows us to load the getCardValue function into tests in other files.
@@ -39,19 +44,24 @@ assertEquals(aceofSpades, 11);
3944
// When the function is called with such a card,
4045
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4146
const fiveofHearts = getCardValue("5♥");
42-
// ====> write your test here, and then add a line to pass the test in the function above
47+
assertEquals(fiveofHearts, 5);
4348

4449
// Handle Face Cards (J, Q, K):
4550
// Given a card with a rank of "10," "J," "Q," or "K",
4651
// When the function is called with such a card,
4752
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
53+
const faceCard = getCardValue("J♣");
54+
assertEquals(faceCard, 10);
4855

4956
// Handle Ace (A):
5057
// Given a card with a rank of "A",
5158
// When the function is called with an Ace,
5259
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
60+
const aceCard = getCardValue("A♦");
61+
assertEquals(aceCard, 11);
5362

5463
// Handle Invalid Cards:
5564
// Given a card with an invalid rank (neither a number nor a recognized face card),
5665
// When the function is called with such a card,
5766
// Then it should throw an error indicating "Invalid card rank."
67+
// const invalidCard = getCardValue("JK");

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,27 @@ test("should identify right angle (90°)", () => {
1212
// Case 2: Identify Acute Angles:
1313
// When the angle is less than 90 degrees,
1414
// Then the function should return "Acute angle"
15+
test("should identify acute angle (less than 90°)", () => {
16+
expect(getAngleType(80)).toEqual("Acute angle");
17+
});
1518

1619
// Case 3: Identify Obtuse Angles:
1720
// When the angle is greater than 90 degrees and less than 180 degrees,
1821
// Then the function should return "Obtuse angle"
22+
test("should identify obtuse angle (greater than 90° & less than 180°)", () => {
23+
expect(getAngleType(130)).toEqual("Obtuse angle");
24+
});
1925

2026
// Case 4: Identify Straight Angles:
2127
// When the angle is exactly 180 degrees,
2228
// Then the function should return "Straight angle"
29+
test("should identify straight angle (180°)", () => {
30+
expect(getAngleType(180)).toEqual("Straight angle");
31+
});
2332

2433
// Case 5: Identify Reflex Angles:
2534
// When the angle is greater than 180 degrees and less than 360 degrees,
2635
// Then the function should return "Reflex angle"
36+
test("should identify reflex angle (greater than 180°)", () => {
37+
expect(getAngleType(280)).toEqual("Reflex angle");
38+
});

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,19 @@ test("should return true for a proper fraction", () => {
77
});
88

99
// Case 2: Identify Improper Fractions:
10+
test("should return false for improper fraction", () => {
11+
expect(isProperFraction(3, 2)).toEqual(false);
12+
});
1013

1114
// Case 3: Identify Negative Fractions:
15+
test("should return true for proper negative fraction", () => {
16+
expect(isProperFraction(-3, 6)).toEqual(true);
17+
});
18+
test("should return false for improper negative fraction", () => {
19+
expect(isProperFraction(-5, 2)).toEqual(false);
20+
});
1221

1322
// Case 4: Identify Equal Numerator and Denominator:
23+
test("should return false for improper fraction (numerator === denominator)", () => {
24+
expect(isProperFraction(3, 3)).toEqual(false);
25+
});

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,30 @@ test("should return 11 for Ace of Spades", () => {
88
});
99

1010
// Case 2: Handle Number Cards (2-10):
11+
test("should return the appropriate number from 2 to 10", () => {
12+
expect(getCardValue("2♠")).toEqual(2);
13+
expect(getCardValue("3♠")).toEqual(3);
14+
expect(getCardValue("4♠")).toEqual(4);
15+
expect(getCardValue("5♠")).toEqual(5);
16+
expect(getCardValue("6♠")).toEqual(6);
17+
expect(getCardValue("7♠")).toEqual(7);
18+
expect(getCardValue("8♠")).toEqual(8);
19+
expect(getCardValue("9♠")).toEqual(9);
20+
expect(getCardValue("10♠")).toEqual(10);
21+
});
1122
// Case 3: Handle Face Cards (J, Q, K):
23+
test("should return 10 for face cards", () => {
24+
expect(getCardValue("J♠")).toEqual(10);
25+
expect(getCardValue("Q♠")).toEqual(10);
26+
expect(getCardValue("K♠")).toEqual(10);
27+
});
1228
// Case 4: Handle Ace (A):
29+
test("should return 11 for Ace (A)", () => {
30+
expect(getCardValue("A♣")).toEqual(11);
31+
expect(getCardValue("A♦")).toEqual(11);
32+
expect(getCardValue("A♥")).toEqual(11);
33+
});
1334
// Case 5: Handle Invalid Cards:
35+
test("Should return 'Invalid card rank.' for invalid cards", () => {
36+
expect(() => getCardValue("KJ")).toThrow("Invalid card rank.");
37+
});

Sprint-3/2-practice-tdd/count.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
return stringOfCharacters
3+
.split("")
4+
.reduce((acc, curr) => acc + (curr === findCharacter ? 1 : 0), 0);
35
}
46

57
module.exports = countChar;

Sprint-3/2-practice-tdd/count.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ 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+
test("should return 0, since there are no occurrences of a character", () => {
26+
const char = "a";
27+
const str = "AABBFFSAA";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(0);
30+
});
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
if ([11, 12, 13].includes(num)) return `${num}th`;
3+
const lastDigit = String(num).slice(-1);
4+
const restOfNum = String(num).slice(0, -1);
5+
let ordinalResult = "";
6+
switch (lastDigit) {
7+
case "1":
8+
ordinalResult = restOfNum + "1st";
9+
break;
10+
case "2":
11+
ordinalResult = restOfNum + "2nd";
12+
break;
13+
case "3":
14+
ordinalResult = restOfNum + "3rd";
15+
break;
16+
default:
17+
ordinalResult = restOfNum + lastDigit + "th";
18+
}
19+
return ordinalResult;
320
}
421

522
module.exports = getOrdinalNumber;

Sprint-3/2-practice-tdd/get-ordinal-number.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,29 @@ const getOrdinalNumber = require("./get-ordinal-number");
1111
test("should return '1st' for 1", () => {
1212
expect(getOrdinalNumber(1)).toEqual("1st");
1313
});
14+
15+
test("should return '2nd' for 2", () => {
16+
expect(getOrdinalNumber(2)).toEqual("2nd");
17+
});
18+
19+
test("should return '3rd' for 3", () => {
20+
expect(getOrdinalNumber(3)).toEqual("3rd");
21+
});
22+
test("should return '4th' for 4", () => {
23+
expect(getOrdinalNumber(4)).toEqual("4th");
24+
});
25+
test("should return '5th' for 5", () => {
26+
expect(getOrdinalNumber(5)).toEqual("5th");
27+
});
28+
test("should return '11th' for 11", () => {
29+
expect(getOrdinalNumber(11)).toEqual("11th");
30+
});
31+
test("should return '21st' for 21", () => {
32+
expect(getOrdinalNumber(21)).toEqual("21st");
33+
});
34+
test("should return '32nd' for 32", () => {
35+
expect(getOrdinalNumber(32)).toEqual("32nd");
36+
});
37+
test("should return '53rd' for 53", () => {
38+
expect(getOrdinalNumber(53)).toEqual("53rd");
39+
});

0 commit comments

Comments
 (0)