Skip to content

Commit b1be443

Browse files
committed
Updated tests
1 parent 06c3aa1 commit b1be443

6 files changed

Lines changed: 120 additions & 235 deletions

File tree

Lines changed: 16 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,27 @@
1-
// Implement a function getAngleType
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.
16-
171
function getAngleType(angle) {
18-
// TODO: Implement this function
2+
if (angle <= 0 || angle >= 360) {
3+
return "Invalid angle";
4+
}
195

206
if (angle > 0 && angle < 90) {
217
return "Acute angle";
22-
} else if (angle === 90) {
8+
}
9+
10+
if (angle === 90) {
2311
return "Right angle";
24-
} else if (angle > 90 && angle < 180) {
12+
}
13+
14+
if (angle > 90 && angle < 180) {
2515
return "Obtuse angle";
26-
} else if (angle === 180) {
27-
return "Straight angle";
28-
} else if (angle > 180 && angle < 360) {
29-
return "Reflex angle";
30-
} else {
31-
return "Invalid angle";
3216
}
33-
}
3417

35-
// The line below allows us to load the getAngleType function into tests in other files.
36-
// This will be useful in the "rewrite tests with jest" step.
37-
module.exports = getAngleType;
18+
if (angle === 180) {
19+
return "Straight angle";
20+
}
3821

39-
// This helper function is written to make our assertions easier to read.
40-
// If the actual output matches the target output, the test will pass
41-
function assertEquals(actualOutput, targetOutput) {
42-
console.assert(
43-
actualOutput === targetOutput,
44-
`Expected ${actualOutput} to equal ${targetOutput}`
45-
);
22+
if (angle > 180 && angle < 360) {
23+
return "Reflex angle";
24+
}
4625
}
4726

48-
// TODO: Write tests to cover all cases, including boundary and invalid cases.
49-
// Example: Identify Right Angles
50-
const right = getAngleType(90);
51-
assertEquals(right, "Right angle");
52-
53-
// Acute angle test
54-
const acute = getAngleType(45);
55-
assertEquals(acute, "Acute angle");
56-
57-
// Obtuse angle test
58-
const obtuse = getAngleType(120);
59-
assertEquals(obtuse, "Obtuse angle");
60-
61-
// Straight angle test
62-
const straight = getAngleType(180);
63-
assertEquals(straight, "Straight angle");
64-
65-
// Reflex angle test
66-
const reflex = getAngleType(270);
67-
assertEquals(reflex, "Reflex angle");
68-
69-
// Boundary tests
70-
assertEquals(getAngleType(1), "Acute angle");
71-
assertEquals(getAngleType(359), "Reflex angle");
72-
73-
// Invalid angle tests
74-
assertEquals(getAngleType(0), "Invalid angle");
75-
assertEquals(getAngleType(360), "Invalid angle");
76-
assertEquals(getAngleType(-10), "Invalid angle");
77-
assertEquals(getAngleType(500), "Invalid angle");
27+
module.exports = getAngleType;
Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,9 @@
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.
12-
131
function isProperFraction(numerator, denominator) {
14-
// TODO: Implement this function
15-
162
if (denominator === 0) {
173
return false;
184
}
195

20-
if (Math.abs(numerator) < Math.abs(denominator)) {
21-
return true;
22-
}
23-
24-
return false;
6+
return Math.abs(numerator) < Math.abs(denominator);
257
}
268

27-
// The line below allows us to load the isProperFraction function into tests in other files.
28-
// This will be useful in the "rewrite tests with jest" step.
29-
module.exports = isProperFraction;
30-
31-
// Here's our helper again
32-
function assertEquals(actualOutput, targetOutput) {
33-
console.assert(
34-
actualOutput === targetOutput,
35-
`Expected ${actualOutput} to equal ${targetOutput}`
36-
);
37-
}
38-
39-
// TODO: Write tests to cover all cases.
40-
// What combinations of numerators and denominators should you test?
41-
42-
// Example: 1/2 is a proper fraction
43-
assertEquals(isProperFraction(1, 2), true);
44-
45-
// numerator smaller than denominator
46-
assertEquals(isProperFraction(3, 4), true);
47-
48-
// numerator equal to denominator (not proper)
49-
assertEquals(isProperFraction(5, 5), false);
50-
51-
// numerator larger than denominator
52-
assertEquals(isProperFraction(7, 3), false);
53-
54-
// numerator is zero
55-
assertEquals(isProperFraction(0, 5), true);
56-
57-
// denominator is zero (invalid)
58-
assertEquals(isProperFraction(1, 0), false);
59-
60-
// negative numbers
61-
assertEquals(isProperFraction(-1, 2), true);
62-
assertEquals(isProperFraction(1, -2), true);
63-
assertEquals(isProperFraction(-3, -2), false);
9+
module.exports = isProperFraction;
Lines changed: 8 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,4 @@
1-
// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck
2-
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-
241
function getCardValue(card) {
25-
// TODO: Implement this function
26-
272
const suits = ["♠", "♥", "♦", "♣"];
283

294
if (typeof card !== "string" || card.length < 2) {
@@ -37,64 +12,21 @@ function getCardValue(card) {
3712
throw new Error("Invalid card");
3813
}
3914

15+
// Ace
4016
if (rank === "A") return 11;
17+
18+
// Face cards
4119
if (["J", "Q", "K"].includes(rank)) return 10;
4220

21+
// Number cards (STRICT check)
4322
const validNumbers = ["2", "3", "4", "5", "6", "7", "8", "9", "10"];
44-
45-
if (!validNumbers.includes(rank)) {
23+
24+
if (validNumbers.includes(rank)) {
4625
return Number(rank);
4726
}
4827

49-
28+
// Everything else = invalid
5029
throw new Error("Invalid card");
5130
}
5231

53-
// The line below allows us to load the getCardValue function into tests in other files.
54-
// This will be useful in the "rewrite tests with jest" step.
55-
module.exports = getCardValue;
56-
57-
// Helper functions to make our assertions easier to read.
58-
function assertEquals(actualOutput, targetOutput) {
59-
console.assert(
60-
actualOutput === targetOutput,
61-
`Expected ${actualOutput} to equal ${targetOutput}`
62-
);
63-
}
64-
65-
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
66-
// Examples:
67-
assertEquals(getCardValue("9♠"), 9);
68-
69-
// Ace
70-
assertEquals(getCardValue("A♠"), 11);
71-
72-
// Face cards
73-
assertEquals(getCardValue("J♥"), 10);
74-
assertEquals(getCardValue("Q♦"), 10);
75-
assertEquals(getCardValue("K♣"), 10);
76-
77-
// Number cards
78-
assertEquals(getCardValue("2♠"), 2);
79-
assertEquals(getCardValue("10♥"), 10);
80-
81-
// Handling invalid cards
82-
try {
83-
getCardValue("invalid");
84-
85-
// This line will not be reached if an error is thrown as expected
86-
console.error("Error was not thrown for invalid card");
87-
} catch (e) {}
88-
89-
// More invalid cases
90-
try {
91-
getCardValue("11♠");
92-
console.error("Error was not thrown for invalid card");
93-
} catch (e) {}
94-
95-
try {
96-
getCardValue("A");
97-
console.error("Error was not thrown for invalid card");
98-
} catch (e) {}
99-
100-
// What other invalid card cases can you think of?
32+
module.exports = getCardValue;
Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
1-
// This statement loads the getAngleType function you wrote in the implement directory.
2-
// We will use the same function, but write tests for it using Jest in this file.
31
const getAngleType = require("../implement/1-get-angle-type");
42

5-
// TODO: Write tests in Jest syntax to cover all cases/outcomes,
6-
// including boundary and invalid cases.
3+
describe("getAngleType", () => {
4+
test("returns Acute angle", () => {
5+
expect(getAngleType(1)).toBe("Acute angle");
6+
expect(getAngleType(45)).toBe("Acute angle");
7+
expect(getAngleType(89)).toBe("Acute angle");
8+
});
79

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-
});
10+
test("returns Right angle", () => {
11+
expect(getAngleType(90)).toBe("Right angle");
12+
});
1513

16-
// Case 2: Right angle
17-
// Case 3: Obtuse angles
18-
// Case 4: Straight angle
19-
// Case 5: Reflex angles
20-
// Case 6: Invalid angles
14+
test("returns Obtuse angle", () => {
15+
expect(getAngleType(100)).toBe("Obtuse angle");
16+
expect(getAngleType(179)).toBe("Obtuse angle");
17+
});
18+
19+
test("returns Straight angle", () => {
20+
expect(getAngleType(180)).toBe("Straight angle");
21+
});
22+
23+
test("returns Reflex angle", () => {
24+
expect(getAngleType(200)).toBe("Reflex angle");
25+
expect(getAngleType(359)).toBe("Reflex angle");
26+
});
27+
28+
test("returns Invalid angle", () => {
29+
expect(getAngleType(0)).toBe("Invalid angle");
30+
expect(getAngleType(360)).toBe("Invalid angle");
31+
expect(getAngleType(-10)).toBe("Invalid angle");
32+
});
33+
});
Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
1-
// This statement loads the isProperFraction function you wrote in the implement directory.
2-
// We will use the same function, but write tests for it using Jest in this file.
31
const isProperFraction = require("../implement/2-is-proper-fraction");
42

5-
// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
3+
describe("isProperFraction", () => {
4+
test("returns true when numerator is smaller than denominator", () => {
5+
expect(isProperFraction(1, 2)).toBe(true);
6+
expect(isProperFraction(3, 4)).toBe(true);
7+
});
68

7-
// Special case: numerator is zero
8-
test(`should return false when denominator is zero`, () => {
9-
expect(isProperFraction(1, 0)).toEqual(false);
10-
});
9+
test("returns false when numerator equals denominator", () => {
10+
expect(isProperFraction(5, 5)).toBe(false);
11+
});
12+
13+
test("returns false when numerator is larger than denominator", () => {
14+
expect(isProperFraction(7, 3)).toBe(false);
15+
});
16+
17+
test("returns true when numerator is zero", () => {
18+
expect(isProperFraction(0, 5)).toBe(true);
19+
});
20+
21+
test("returns false when denominator is zero", () => {
22+
expect(isProperFraction(1, 0)).toBe(false);
23+
});
24+
25+
test("works with negative numbers", () => {
26+
expect(isProperFraction(-1, 2)).toBe(true);
27+
expect(isProperFraction(1, -2)).toBe(true);
28+
expect(isProperFraction(-3, -2)).toBe(false);
29+
});
30+
});

0 commit comments

Comments
 (0)