|
1 | 1 | // Implement a function getAngleType |
2 | 2 | // |
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: |
4 | 4 | // - "Acute angle" for angles greater than 0° and less than 90° |
5 | 5 | // - "Right angle" for exactly 90° |
6 | 6 | // - "Obtuse angle" for angles greater than 90° and less than 180° |
7 | 7 | // - "Straight angle" for exactly 180° |
8 | 8 | // - "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 |
10 | 10 |
|
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.) |
12 | 12 |
|
13 | 13 | // 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 |
15 | 15 | // execute the code to ensure all tests pass. |
16 | 16 |
|
17 | 17 | 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 | + } |
19 | 31 | } |
20 | 32 |
|
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 |
23 | 35 | module.exports = getAngleType; |
24 | 36 |
|
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 |
27 | 39 | function assertEquals(actualOutput, targetOutput) { |
28 | 40 | console.assert( |
29 | 41 | actualOutput === targetOutput, |
30 | 42 | `Expected ${actualOutput} to equal ${targetOutput}` |
31 | 43 | ); |
32 | 44 | } |
33 | 45 |
|
34 | | -// TODO: Write tests to cover all cases, including boundary and invalid cases. |
| 46 | +// TODO: Write tests to cover all cases, including boundary values |
35 | 47 | // Example: Identify Right Angles |
36 | 48 | const right = getAngleType(90); |
37 | 49 | 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 | + |
0 commit comments