Skip to content

Commit 4ab52ef

Browse files
Resolve conflicts for Sprint-3
2 parents ea75dd2 + c64bb45 commit 4ab52ef

File tree

11 files changed

+266
-162
lines changed

11 files changed

+266
-162
lines changed

Sprint-3/1-implement-and-rewrite-tests/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Implement solutions and rewrite tests with Jest
22

3+
Before writing any code, please read the [Testing Function Guide](testing-guide.md) to learn how
4+
to choose test values that thoroughly test a function.
5+
36
## 1 Implement solutions
47

58
In the `implement` directory you've got a number of functions you'll need to implement.
Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,62 @@
11
// Implement a function getAngleType
2-
// Build up your function case by case, writing tests as you go
3-
// The first test and case is written for you. The next case has a test, but no code.
4-
// Execute this script in your terminal
5-
// node 1-get-angle-type.js
6-
// The assertion error will tell you what the expected output is
7-
// Write the code to pass the test
8-
// Then, write the next test! :) Go through this process until all the cases are implemented
2+
//
3+
// When given an angle in degrees, it should return a string indicating the type of angle:
4+
// - "Acute angle" for angles greater than 0° and less than 90°
5+
// - "Right angle" for exactly 90°
6+
// - "Obtuse angle" for angles greater than 90° and less than 180°
7+
// - "Straight angle" for exactly 180°
8+
// - "Reflex angle" for angles greater than 180° and less than 360°
9+
// - "Invalid angle" for angles outside the valid range.
10+
11+
// Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.)
12+
13+
// Acceptance criteria:
14+
// After you have implemented the function, write tests to cover all the cases, and
15+
// execute the code to ensure all tests pass.
916

1017
function getAngleType(angle) {
18+
if (typeof angle !== "number" || angle <= 0 || angle >= 360)
19+
return "Invalid angle";
1120
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.
21+
if (angle < 90) return "Acute angle";
22+
if (angle > 90 && angle < 180) return "Obtuse angle";
23+
if (angle === 180) return "Straight angle";
24+
return "Reflex angle";
1825
}
26+
console.log();
1927

2028
// The line below allows us to load the getAngleType function into tests in other files.
2129
// This will be useful in the "rewrite tests with jest" step.
2230
module.exports = getAngleType;
2331

24-
// we're going to use this helper function to make our assertions easier to read
25-
// if the actual output matches the target output, the test will pass
32+
// This helper function is written to make our assertions easier to read.
33+
// If the actual output matches the target output, the test will pass
2634
function assertEquals(actualOutput, targetOutput) {
2735
console.assert(
2836
actualOutput === targetOutput,
2937
`Expected ${actualOutput} to equal ${targetOutput}`
3038
);
3139
}
3240

33-
// Acceptance criteria:
34-
35-
// Given an angle in degrees,
36-
// When the function getAngleType is called with this angle,
37-
// Then it should:
38-
39-
// Case 1: Identify Right Angles:
40-
// When the angle is exactly 90 degrees,
41-
// Then the function should return "Right angle"
41+
// TODO: Write tests to cover all cases, including boundary and invalid cases.
42+
// Example: Identify Right Angles
4243
const right = getAngleType(90);
4344
assertEquals(right, "Right angle");
4445

45-
// Case 2: Identify Acute Angles:
46-
// When the angle is less than 90 degrees,
47-
// Then the function should return "Acute angle"
4846
const acute = getAngleType(45);
4947
assertEquals(acute, "Acute angle");
5048

51-
// Case 3: Identify Obtuse Angles:
52-
// When the angle is greater than 90 degrees and less than 180 degrees,
53-
// Then the function should return "Obtuse angle"
5449
const obtuse = getAngleType(120);
5550
assertEquals(obtuse, "Obtuse angle");
5651

57-
// Case 4: Identify Straight Angles:
58-
// When the angle is exactly 180 degrees,
59-
// Then the function should return "Straight angle"
6052
const straight = getAngleType(180);
6153
assertEquals(straight, "Straight angle");
6254

63-
// Case 5: Identify Reflex Angles:
64-
// When the angle is greater than 180 degrees and less than 360 degrees,
65-
// Then the function should return "Reflex angle"
6655
const reflex = getAngleType(260);
6756
assertEquals(reflex, "Reflex angle");
57+
58+
const invalid1 = getAngleType(400);
59+
assertEquals(invalid1, "Invalid angle");
60+
61+
const invalid2 = getAngleType("400");
62+
assertEquals(invalid2, "Invalid angle");
Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
// Implement a function isProperFraction
2-
// Write assertions for your function to check it works in different cases
3-
// Terms:
4-
// Fractions: https://www.bbc.co.uk/bitesize/topics/zt9n6g8/articles/zjxpp4j
5-
// Written here like this: 1/2 == Numerator/Denominator
6-
// the first test and first case is written for you
7-
// complete the rest of the tests and cases
8-
// write one test at a time, and make it pass, build your solution up methodically
1+
// Implement a function isProperFraction,
2+
// when given two numbers, a numerator and a denominator, it should return true if
3+
// the given numbers form a proper fraction, and false otherwise.
4+
5+
// Assumption: The parameters are valid numbers (not NaN or Infinity).
6+
7+
// Note: If you are unfamiliar with proper fractions, please look up its mathematical definition.
8+
9+
// Acceptance criteria:
10+
// After you have implemented the function, write tests to cover all the cases, and
11+
// execute the code to ensure all tests pass.
912

1013
function isProperFraction(numerator, denominator) {
1114
numerator = Math.abs(numerator);
@@ -19,53 +22,36 @@ function isProperFraction(numerator, denominator) {
1922
// This will be useful in the "rewrite tests with jest" step.
2023
module.exports = isProperFraction;
2124

22-
// here's our helper again
25+
// Here's our helper again
2326
function assertEquals(actualOutput, targetOutput) {
2427
console.assert(
2528
actualOutput === targetOutput,
2629
`Expected ${actualOutput} to equal ${targetOutput}`
2730
);
2831
}
2932

30-
// Acceptance criteria:
33+
// TODO: Write tests to cover all cases.
34+
// What combinations of numerators and denominators should you test?
3135

32-
// Proper Fraction check:
33-
// Input: numerator = 2, denominator = 3
34-
// target output: true
35-
// Explanation: The fraction 2/3 is a proper fraction, where the numerator is less than the denominator. The function should return true.
36-
const properFraction = isProperFraction(2, 3);
37-
assertEquals(properFraction, true);
36+
// Example: 1/2 is a proper fraction
37+
assertEquals(isProperFraction(1, 2), true);
3838

39-
// Improper Fraction check:
40-
// Input: numerator = 5, denominator = 2
41-
// target output: false
42-
// 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+
// Example: 5/2 is not a proper fraction
4340
const improperFraction = isProperFraction(5, 2);
4441
assertEquals(improperFraction, false);
4542

46-
// Negative Fraction check:
47-
// Input: numerator = -4, denominator = 7
48-
// target output: true
49-
// 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.
43+
// Example: -4/7 is a proper fraction
5044
const negativeFraction = isProperFraction(-4, 7);
5145
assertEquals(negativeFraction, true);
5246

53-
// Equal Numerator and Denominator check:
54-
// Input: numerator = 3, denominator = 3
55-
// target output: false
56-
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false.
47+
// Example: 3/3 is not a proper fraction
5748
const equalFraction = isProperFraction(3, 3);
5849
assertEquals(equalFraction, false);
5950

60-
// Stretch:
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.
51+
// Example: 4/-7 is a proper fraction
6452
const negativeFraction1 = isProperFraction(4, -7);
6553
assertEquals(negativeFraction1, true);
6654

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.
55+
// Example: -4/-7 is a proper fraction
7056
const negativeFraction2 = isProperFraction(4, 7);
7157
assertEquals(negativeFraction2, true);
Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck
22

3-
// You will need to implement a function getCardValue
4-
// the function takes a single parameter, a string representing a playing card
5-
// the function should return the numerical value of the card
6-
// the first test and first case is written for you
7-
// complete the rest of the tests and cases
8-
// write one test at a time, and make it pass, build your solution up methodically
9-
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
3+
// Implement a function getCardValue, when given a string representing a playing card,
4+
// should return the numerical value of the card.
5+
6+
// A valid card string will contain a rank followed by the suit.
7+
// The rank can be one of the following strings:
8+
// "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
9+
// The suit can be one of the following emojis:
10+
// "♠", "♥", "♦", "♣"
11+
// For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦".
12+
13+
// When the card is an ace ("A"), the function should return 11.
14+
// When the card is a face card ("J", "Q", "K"), the function should return 10.
15+
// When the card is a number card ("2" to "10"), the function should return its numeric value.
16+
17+
// When the card string is invalid (not following the above format), the function should
18+
// throw an error.
19+
20+
// Acceptance criteria:
21+
// After you have implemented the function, write tests to cover all the cases, and
22+
// execute the code to ensure all tests pass.
23+
1024
function getCardValue(card) {
1125
if (!["♠", "♣", "♦", "♥"].includes(card.slice(-1)))
1226
throw new Error("Invalid card rank.");
@@ -22,46 +36,32 @@ function getCardValue(card) {
2236
// This will be useful in the "rewrite tests with jest" step.
2337
module.exports = getCardValue;
2438

25-
// You need to write assertions for your function to check it works in different cases
26-
// we're going to use this helper function to make our assertions easier to read
27-
// if the actual output matches the target output, the test will pass
39+
// Helper functions to make our assertions easier to read.
2840
function assertEquals(actualOutput, targetOutput) {
2941
console.assert(
3042
actualOutput === targetOutput,
3143
`Expected ${actualOutput} to equal ${targetOutput}`
3244
);
3345
}
34-
// Acceptance criteria:
3546

36-
// Given a card string in the format "A♠" (representing a card in blackjack - the last character will always be an emoji for a suit, and all characters before will be a number 2-10, or one letter of J, Q, K, A),
37-
// When the function getCardValue is called with this card string as input,
38-
// Then it should return the numerical card value
39-
const aceofSpades = getCardValue("A♠");
40-
assertEquals(aceofSpades, 11);
47+
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
48+
// Examples:
49+
assertEquals(getCardValue("9♠"), 9);
50+
51+
// Handling invalid cards
52+
try {
53+
getCardValue("invalid");
4154

42-
// Handle Number Cards (2-10):
43-
// Given a card with a rank between "2" and "9",
44-
// When the function is called with such a card,
45-
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
46-
const fiveofHearts = getCardValue("5♥");
47-
assertEquals(fiveofHearts, 5);
55+
// This line will not be reached if an error is thrown as expected
56+
console.error("Error was not thrown for invalid card");
57+
} catch (e) {}
58+
59+
// What other invalid card cases can you think of?
60+
const fiveOfHearts = getCardValue("5♥");
61+
assertEquals(fiveOfHearts, 5);
4862

49-
// Handle Face Cards (J, Q, K):
50-
// Given a card with a rank of "10," "J," "Q," or "K",
51-
// When the function is called with such a card,
52-
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
5363
const faceCard = getCardValue("J♣");
5464
assertEquals(faceCard, 10);
5565

56-
// Handle Ace (A):
57-
// Given a card with a rank of "A",
58-
// When the function is called with an Ace,
59-
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
6066
const aceCard = getCardValue("A♦");
6167
assertEquals(aceCard, 11);
62-
63-
// Handle Invalid Cards:
64-
// Given a card with an invalid rank (neither a number nor a recognized face card),
65-
// When the function is called with such a card,
66-
// 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: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,43 @@
22
// We will use the same function, but write tests for it using Jest in this file.
33
const getAngleType = require("../implement/1-get-angle-type");
44

5-
test("should identify right angle (90°)", () => {
6-
expect(getAngleType(90)).toEqual("Right angle");
7-
});
5+
// TODO: Write tests in Jest syntax to cover all cases/outcomes,
6+
// including boundary and invalid cases.
87

9-
// REPLACE the comments with the tests
10-
// make your test descriptions as clear and readable as possible
8+
// Case 1: Acute angles
9+
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
10+
// Test various acute angles, including boundary cases
11+
expect(getAngleType(1)).toEqual("Acute angle");
12+
expect(getAngleType(45)).toEqual("Acute angle");
13+
expect(getAngleType(89)).toEqual("Acute angle");
14+
});
1115

12-
// Case 2: Identify Acute Angles:
13-
// When the angle is less than 90 degrees,
14-
// Then the function should return "Acute angle"
15-
test("should identify acute angle (less than 90°)", () => {
16-
expect(getAngleType(80)).toEqual("Acute angle");
16+
// Case 2: Right angle
17+
test(`should return "Acute angle" when (angle = 90)`, () => {
18+
expect(getAngleType(90)).toEqual("Right angle");
1719
});
1820

19-
// Case 3: Identify Obtuse Angles:
20-
// When the angle is greater than 90 degrees and less than 180 degrees,
21-
// Then the function should return "Obtuse angle"
22-
test("should identify obtuse angle (greater than 90° & less than 180°)", () => {
21+
// Case 3: Obtuse angles
22+
test("should identify obtuse angle (90° < angle < 180°)", () => {
23+
expect(getAngleType(91)).toEqual("Obtuse angle");
2324
expect(getAngleType(130)).toEqual("Obtuse angle");
25+
expect(getAngleType(179)).toEqual("Obtuse angle");
2426
});
2527

26-
// Case 4: Identify Straight Angles:
27-
// When the angle is exactly 180 degrees,
28-
// Then the function should return "Straight angle"
29-
test("should identify straight angle (180°)", () => {
28+
// Case 4: Straight angle
29+
test("should identify straight angle (angle = 180°)", () => {
3030
expect(getAngleType(180)).toEqual("Straight angle");
3131
});
3232

33-
// Case 5: Identify Reflex Angles:
34-
// When the angle is greater than 180 degrees and less than 360 degrees,
35-
// Then the function should return "Reflex angle"
36-
test("should identify reflex angle (greater than 180°)", () => {
33+
// Case 5: Reflex angles
34+
test("should identify reflex angle (180° < angle < 360)", () => {
35+
expect(getAngleType(181)).toEqual("Reflex angle");
3736
expect(getAngleType(280)).toEqual("Reflex angle");
37+
expect(getAngleType(359)).toEqual("Reflex angle");
38+
});
39+
40+
// Case 6: Invalid angles
41+
test("should identify invalid angle (0 > angle > 360)", () => {
42+
expect(getAngleType(0)).toEqual("Invalid angle");
43+
expect(getAngleType(361)).toEqual("Invalid angle");
3844
});

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
// We will use the same function, but write tests for it using Jest in this file.
33
const isProperFraction = require("../implement/2-is-proper-fraction");
44

5-
test("should return true for a proper fraction", () => {
6-
expect(isProperFraction(2, 3)).toEqual(true);
5+
// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
6+
7+
// Special case: numerator is zero
8+
test(`should return false when denominator is zero`, () => {
9+
expect(isProperFraction(1, 0)).toEqual(false);
710
});
811

912
// Case 2: Identify Improper Fractions:
1013
test("should return false for improper fraction", () => {
11-
expect(isProperFraction(3, 2)).toEqual(false);
14+
expect(isProperFraction(2, 3)).toEqual(true);
1215
});
1316

1417
// Case 3: Identify Negative Fractions:

0 commit comments

Comments
 (0)