Skip to content

Commit 1f53f87

Browse files
committed
Implement getAngleType and add tests
1 parent b31a586 commit 1f53f87

2 files changed

Lines changed: 56 additions & 11 deletions

File tree

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,55 @@
11
// Implement a function getAngleType
22
//
3-
// When given an angle in degrees, it should return a string indicating the type of angle:
3+
// When given an angle in degrees, it should return a string:
44
// - "Acute angle" for angles greater than 0° and less than 90°
55
// - "Right angle" for exactly 90°
66
// - "Obtuse angle" for angles greater than 90° and less than 180°
77
// - "Straight angle" for exactly 180°
88
// - "Reflex angle" for angles greater than 180° and less than 360°
9-
// - "Invalid angle" for angles outside the valid range.
9+
// - "Invalid angle" for angles outside the valid range
1010

11-
// Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.)
11+
// Assumption: The parameter is a valid number. (You do not need to check this.)
1212

1313
// Acceptance criteria:
14-
// After you have implemented the function, write tests to cover all the cases, and
14+
// After you have implemented the function, write tests to cover all cases, including boundary values, and
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+
} else if (angle === 90) {
21+
return "Right angle";
22+
} else if (angle > 90 && angle < 180) {
23+
return "Obtuse angle";
24+
} else if (angle === 180) {
25+
return "Straight angle";
26+
} else if (angle > 180 && angle < 360) {
27+
return "Reflex angle";
28+
} else {
29+
return "Invalid angle";
30+
}
1931
}
2032

21-
// The line below allows us to load the getAngleType function into tests in other files.
22-
// This will be useful in the "rewrite tests with jest" step.
33+
// The line below allows us to load the getAngleType function into other files
34+
// This will be useful in the "rewrite tests with jest" step later
2335
module.exports = getAngleType;
2436

25-
// This helper function is written to make our assertions easier to read.
26-
// If the actual output matches the target output, the test will pass
37+
// This helper function is written to make our assertions easier to read
38+
// If the actual output matches the target output, the test passes
2739
function assertEquals(actualOutput, targetOutput) {
2840
console.assert(
2941
actualOutput === targetOutput,
3042
`Expected ${actualOutput} to equal ${targetOutput}`
3143
);
3244
}
3345

34-
// TODO: Write tests to cover all cases, including boundary and invalid cases.
46+
// TODO: Write tests to cover all cases, including boundary values
3547
// Example: Identify Right Angles
3648
const right = getAngleType(90);
3749
assertEquals(right, "Right angle");
50+
const acute = getAngleType(45);
51+
assertEquals(acute, "Acute angle");
52+
53+
const invalid = getAngleType(-10);
54+
assertEquals(invalid, "Invalid angle");
55+

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,42 @@ const getAngleType = require("../implement/1-get-angle-type");
66
// including boundary and invalid cases.
77

88
// Case 1: Acute angles
9-
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
9+
test("should return \"Acute angle\" when (0 < angle < 90)", () => {
1010
// Test various acute angles, including boundary cases
1111
expect(getAngleType(1)).toEqual("Acute angle");
1212
expect(getAngleType(45)).toEqual("Acute angle");
1313
expect(getAngleType(89)).toEqual("Acute angle");
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(135)).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 angles outside the valid range", () => {
42+
expect(getAngleType(0)).toEqual("Invalid angle");
43+
expect(getAngleType(-10)).toEqual("Invalid angle");
44+
expect(getAngleType(360)).toEqual("Invalid angle");
45+
expect(getAngleType(400)).toEqual("Invalid angle");
46+
});
47+

0 commit comments

Comments
 (0)