Skip to content

Commit 657a9cb

Browse files
committed
Complete Sprint 3 mandatory exercises
1 parent b31a586 commit 657a9cb

8 files changed

Lines changed: 141 additions & 30 deletions

File tree

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

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,27 @@
1515
// execute the code to ensure all tests pass.
1616

1717
function getAngleType(angle) {
18-
// TODO: Implement this function
18+
if (angle > 0 && angle < 90) {
19+
return "Acute angle";
20+
}
21+
22+
if (angle === 90) {
23+
return "Right angle";
24+
}
25+
26+
if (angle > 90 && angle < 180) {
27+
return "Obtuse angle";
28+
}
29+
30+
if (angle === 180) {
31+
return "Straight angle";
32+
}
33+
34+
if (angle > 180 && angle < 360) {
35+
return "Reflex angle";
36+
}
37+
38+
return "Invalid angle";
1939
}
2040

2141
// The line below allows us to load the getAngleType function into tests in other files.
@@ -31,7 +51,22 @@ function assertEquals(actualOutput, targetOutput) {
3151
);
3252
}
3353

34-
// TODO: Write tests to cover all cases, including boundary and invalid cases.
35-
// Example: Identify Right Angles
36-
const right = getAngleType(90);
37-
assertEquals(right, "Right angle");
54+
// Acute angle
55+
assertEquals(getAngleType(45), "Acute angle");
56+
57+
// Right angle
58+
assertEquals(getAngleType(90), "Right angle");
59+
60+
// Obtuse angle
61+
assertEquals(getAngleType(120), "Obtuse angle");
62+
63+
// Straight angle
64+
assertEquals(getAngleType(180), "Straight angle");
65+
66+
// Reflex angle
67+
assertEquals(getAngleType(270), "Reflex angle");
68+
69+
// Invalid angles
70+
assertEquals(getAngleType(0), "Invalid angle");
71+
assertEquals(getAngleType(360), "Invalid angle");
72+
assertEquals(getAngleType(-10), "Invalid angle");

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// execute the code to ensure all tests pass.
1212

1313
function isProperFraction(numerator, denominator) {
14-
// TODO: Implement this function
14+
return numerator >= 0 && denominator > 0 && numerator < denominator;
1515
}
1616

1717
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -29,5 +29,12 @@ function assertEquals(actualOutput, targetOutput) {
2929
// TODO: Write tests to cover all cases.
3030
// What combinations of numerators and denominators should you test?
3131

32-
// Example: 1/2 is a proper fraction
32+
// Proper fractions
3333
assertEquals(isProperFraction(1, 2), true);
34+
assertEquals(isProperFraction(3, 4), true);
35+
36+
// Not proper fractions
37+
assertEquals(isProperFraction(2, 2), false);
38+
assertEquals(isProperFraction(5, 2), false);
39+
assertEquals(isProperFraction(-1, 2), false);
40+
assertEquals(isProperFraction(1, 0), false);

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

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,26 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
const rank = card.slice(0, -1);
26+
const suit = card.slice(-1);
27+
28+
if (!["♠", "♥", "♦", "♣"].includes(suit)) {
29+
throw new Error("Invalid card");
30+
}
31+
32+
if (rank === "A") {
33+
return 11;
34+
}
35+
36+
if (rank === "J" || rank === "Q" || rank === "K") {
37+
return 10;
38+
}
39+
40+
if (["2", "3", "4", "5", "6", "7", "8", "9", "10"].includes(rank)) {
41+
return Number(rank);
42+
}
43+
44+
throw new Error("Invalid card");
2645
}
2746

2847
// The line below allows us to load the getCardValue function into tests in other files.
@@ -37,18 +56,36 @@ function assertEquals(actualOutput, targetOutput) {
3756
);
3857
}
3958

40-
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
41-
// Examples:
59+
// Number cards
4260
assertEquals(getCardValue("9♠"), 9);
61+
assertEquals(getCardValue("10♥"), 10);
62+
63+
// Ace
64+
assertEquals(getCardValue("A♣"), 11);
4365

44-
// Handling invalid cards
66+
// Face cards
67+
assertEquals(getCardValue("J♦"), 10);
68+
assertEquals(getCardValue("Q♥"), 10);
69+
assertEquals(getCardValue("K♠"), 10);
70+
71+
// Invalid cards
4572
try {
4673
getCardValue("invalid");
47-
48-
// This line will not be reached if an error is thrown as expected
4974
console.error("Error was not thrown for invalid card 😢");
5075
} catch (e) {
5176
console.log("Error thrown for invalid card 🎉");
5277
}
5378

54-
// What other invalid card cases can you think of?
79+
try {
80+
getCardValue("1♠");
81+
console.error("Error was not thrown for invalid rank 😢");
82+
} catch (e) {
83+
console.log("Error thrown for invalid rank 🎉");
84+
}
85+
86+
try {
87+
getCardValue("AX");
88+
console.error("Error was not thrown for invalid suit 😢");
89+
} catch (e) {
90+
console.log("Error thrown for invalid suit 🎉");
91+
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,32 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
1414
});
1515

1616
// Case 2: Right angle
17+
test("should return 'Right angle' when angle is 90", () => {
18+
expect(getAngleType(90)).toEqual("Right angle");
19+
});
20+
1721
// Case 3: Obtuse angles
22+
test("should return 'Obtuse angle' when 90 < angle < 180", () => {
23+
expect(getAngleType(91)).toEqual("Obtuse angle");
24+
expect(getAngleType(120)).toEqual("Obtuse angle");
25+
expect(getAngleType(179)).toEqual("Obtuse angle");
26+
});
27+
1828
// Case 4: Straight angle
29+
test("should return 'Straight angle' when angle is 180", () => {
30+
expect(getAngleType(180)).toEqual("Straight angle");
31+
});
32+
1933
// Case 5: Reflex angles
34+
test("should return 'Reflex angle' when 180 < angle < 360", () => {
35+
expect(getAngleType(181)).toEqual("Reflex angle");
36+
expect(getAngleType(270)).toEqual("Reflex angle");
37+
expect(getAngleType(359)).toEqual("Reflex angle");
38+
});
39+
2040
// Case 6: Invalid angles
41+
test("should return 'Invalid angle' for invalid values", () => {
42+
expect(getAngleType(0)).toEqual("Invalid angle");
43+
expect(getAngleType(-10)).toEqual("Invalid angle");
44+
expect(getAngleType(360)).toEqual("Invalid angle");
45+
});

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,23 @@ test(`Should return 11 when given an ace card`, () => {
99
expect(getCardValue("A♠")).toEqual(11);
1010
});
1111

12-
// Suggestion: Group the remaining test data into these categories:
13-
// Number Cards (2-10)
14-
// Face Cards (J, Q, K)
15-
// Invalid Cards
12+
// Number Cards
13+
test("Should return the value of number cards", () => {
14+
expect(getCardValue("2♥")).toEqual(2);
15+
expect(getCardValue("9♣")).toEqual(9);
16+
expect(getCardValue("10♦")).toEqual(10);
17+
});
1618

17-
// To learn how to test whether a function throws an error as expected in Jest,
18-
// please refer to the Jest documentation:
19-
// https://jestjs.io/docs/expect#tothrowerror
19+
// Face Cards
20+
test("Should return 10 for face cards", () => {
21+
expect(getCardValue("J♠")).toEqual(10);
22+
expect(getCardValue("Q♥")).toEqual(10);
23+
expect(getCardValue("K♣")).toEqual(10);
24+
});
2025

26+
// Invalid Cards
27+
test("Should throw an error for invalid cards", () => {
28+
expect(() => getCardValue("1♠")).toThrow();
29+
expect(() => getCardValue("invalid")).toThrow();
30+
expect(() => getCardValue("AX")).toThrow();
31+
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
return `${num}st`;
33
}
44

55
module.exports = getOrdinalNumber;

Sprint-3/3-dead-code/exercise-1.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ let testName = "Jerry";
55
const greeting = "hello";
66

77
function sayHello(greeting, name) {
8-
const greetingStr = greeting + ", " + name + "!";
98
return `${greeting}, ${name}!`;
10-
console.log(greetingStr);
119
}
1210

1311
testName = "Aman";

Sprint-3/3-dead-code/exercise-2.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,25 @@
22
// The countAndCapitalisePets function should continue to work for any reasonable input it's given, and you shouldn't modify the pets variable.
33

44
const pets = ["parrot", "hamster", "horse", "dog", "hamster", "cat", "hamster"];
5-
const capitalisedPets = pets.map((pet) => pet.toUpperCase());
6-
const petsStartingWithH = pets.filter((pet) => pet[0] === "h");
75

8-
function logPets(petsArr) {
9-
petsArr.forEach((pet) => console.log(pet));
10-
}
6+
const petsStartingWithH = pets.filter((pet) => pet[0] === "h");
117

128
function countAndCapitalisePets(petsArr) {
139
const petCount = {};
1410

1511
petsArr.forEach((pet) => {
1612
const capitalisedPet = pet.toUpperCase();
13+
1714
if (petCount[capitalisedPet]) {
1815
petCount[capitalisedPet] += 1;
1916
} else {
2017
petCount[capitalisedPet] = 1;
2118
}
2219
});
20+
2321
return petCount;
2422
}
2523

2624
const countedPetsStartingWithH = countAndCapitalisePets(petsStartingWithH);
2725

28-
console.log(countedPetsStartingWithH); // { 'HAMSTER': 3, 'HORSE': 1 } <- Final console log
26+
console.log(countedPetsStartingWithH); // { HAMSTER: 3, HORSE: 1 }

0 commit comments

Comments
 (0)