|
| 1 | +/** |
1 | 2 | // Implement a function getAngleType |
2 | 3 | // |
3 | 4 | // When given an angle in degrees, it should return a string indicating the type of angle: |
@@ -35,3 +36,271 @@ function assertEquals(actualOutput, targetOutput) { |
35 | 36 | // Example: Identify Right Angles |
36 | 37 | const right = getAngleType(90); |
37 | 38 | assertEquals(right, "Right angle"); |
| 39 | +
|
| 40 | +// Solution: |
| 41 | +// Implement a function getAngleType |
| 42 | +// |
| 43 | +// When given an angle in degrees, it should return a string indicating the type of angle: |
| 44 | +// - "Acute angle" for angles greater than 0° and less than 90° |
| 45 | +// - "Right angle" for exactly 90° |
| 46 | +// - "Obtuse angle" for angles greater than 90° and less than 180° |
| 47 | +// - "Straight angle" for exactly 180° |
| 48 | +// - "Reflex angle" for angles greater than 180° and less than 360° |
| 49 | +// - "Invalid angle" for angles outside the valid range. |
| 50 | +
|
| 51 | +// Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.) |
| 52 | +
|
| 53 | +// Acceptance criteria: |
| 54 | +// After you have implemented the function, write tests to cover all the cases, and |
| 55 | +// execute the code to ensure all tests pass. |
| 56 | +
|
| 57 | +function getAngleType(angle) { |
| 58 | + // Check for invalid angles (outside 0-360 range) |
| 59 | + if (angle <= 0 || angle >= 360) { |
| 60 | + return "Invalid angle"; |
| 61 | + } |
| 62 | +
|
| 63 | + // Check for specific angle types |
| 64 | + if (angle > 0 && angle < 90) { |
| 65 | + return "Acute angle"; |
| 66 | + } else if (angle === 90) { |
| 67 | + return "Right angle"; |
| 68 | + } else if (angle > 90 && angle < 180) { |
| 69 | + return "Obtuse angle"; |
| 70 | + } else if (angle === 180) { |
| 71 | + return "Straight angle"; |
| 72 | + } else if (angle > 180 && angle < 360) { |
| 73 | + return "Reflex angle"; |
| 74 | + } |
| 75 | +
|
| 76 | + // This line should theoretically never be reached given our conditions, |
| 77 | + // but included as a safety net |
| 78 | + return "Invalid angle"; |
| 79 | +} |
| 80 | +
|
| 81 | +// The line below allows us to load the getAngleType function into tests in other files. |
| 82 | +// This will be useful in the "rewrite tests with jest" step. |
| 83 | +module.exports = getAngleType; |
| 84 | +
|
| 85 | +// This helper function is written to make our assertions easier to read. |
| 86 | +// If the actual output matches the target output, the test will pass |
| 87 | +function assertEquals(actualOutput, targetOutput) { |
| 88 | + console.assert( |
| 89 | + actualOutput === targetOutput, |
| 90 | + `Expected ${actualOutput} to equal ${targetOutput}` |
| 91 | + ); |
| 92 | +} |
| 93 | +
|
| 94 | +// Write tests to cover all cases, including boundary and invalid cases. |
| 95 | +console.log("Running tests for getAngleType function...\n"); |
| 96 | +
|
| 97 | +// Test 1: Acute angles |
| 98 | +console.log("Testing Acute angles:"); |
| 99 | +const acute1 = getAngleType(45); |
| 100 | +assertEquals(acute1, "Acute angle"); |
| 101 | +
|
| 102 | +const acute2 = getAngleType(89); |
| 103 | +assertEquals(acute2, "Acute angle"); |
| 104 | +
|
| 105 | +const acute3 = getAngleType(1); |
| 106 | +assertEquals(acute3, "Acute angle"); |
| 107 | +
|
| 108 | +// Test 2: Right angle |
| 109 | +console.log("\nTesting Right angle:"); |
| 110 | +const right = getAngleType(90); |
| 111 | +assertEquals(right, "Right angle"); |
| 112 | +
|
| 113 | +// Test 3: Obtuse angles |
| 114 | +console.log("\nTesting Obtuse angles:"); |
| 115 | +const obtuse1 = getAngleType(95); |
| 116 | +assertEquals(obtuse1, "Obtuse angle"); |
| 117 | +
|
| 118 | +const obtuse2 = getAngleType(135); |
| 119 | +assertEquals(obtuse2, "Obtuse angle"); |
| 120 | +
|
| 121 | +const obtuse3 = getAngleType(179); |
| 122 | +assertEquals(obtuse3, "Obtuse angle"); |
| 123 | +
|
| 124 | +// Test 4: Straight angle |
| 125 | +console.log("\nTesting Straight angle:"); |
| 126 | +const straight = getAngleType(180); |
| 127 | +assertEquals(straight, "Straight angle"); |
| 128 | +
|
| 129 | +// Test 5: Reflex angles |
| 130 | +console.log("\nTesting Reflex angles:"); |
| 131 | +const reflex1 = getAngleType(185); |
| 132 | +assertEquals(reflex1, "Reflex angle"); |
| 133 | +
|
| 134 | +const reflex2 = getAngleType(270); |
| 135 | +assertEquals(reflex2, "Reflex angle"); |
| 136 | +
|
| 137 | +const reflex3 = getAngleType(359); |
| 138 | +assertEquals(reflex3, "Reflex angle"); |
| 139 | +
|
| 140 | +// Test 6: Invalid angles (boundary cases and outside range) |
| 141 | +console.log("\nTesting Invalid angles:"); |
| 142 | +
|
| 143 | +// Boundary cases at 0 and 360 |
| 144 | +const invalid1 = getAngleType(0); |
| 145 | +assertEquals(invalid1, "Invalid angle"); |
| 146 | +
|
| 147 | +const invalid2 = getAngleType(360); |
| 148 | +assertEquals(invalid2, "Invalid angle"); |
| 149 | +
|
| 150 | +// Negative angles |
| 151 | +const invalid3 = getAngleType(-45); |
| 152 | +assertEquals(invalid3, "Invalid angle"); |
| 153 | +
|
| 154 | +const invalid4 = getAngleType(-90); |
| 155 | +assertEquals(invalid4, "Invalid angle"); |
| 156 | +
|
| 157 | +const invalid5 = getAngleType(-180); |
| 158 | +assertEquals(invalid5, "Invalid angle"); |
| 159 | +
|
| 160 | +// Angles greater than 360 |
| 161 | +const invalid6 = getAngleType(365); |
| 162 | +assertEquals(invalid6, "Invalid angle"); |
| 163 | +
|
| 164 | +const invalid7 = getAngleType(400); |
| 165 | +assertEquals(invalid7, "Invalid angle"); |
| 166 | +
|
| 167 | +const invalid8 = getAngleType(720); |
| 168 | +assertEquals(invalid8, "Invalid angle"); |
| 169 | +
|
| 170 | +console.log("\nAll tests completed!"); |
| 171 | +* |
| 172 | +*/ |
| 173 | + |
| 174 | +// Writing by me @Carlos Abreu |
| 175 | + |
| 176 | +// Implement a function getAngleType |
| 177 | +// |
| 178 | +// When given an angle in degrees, it should return a string indicating the type of angle: |
| 179 | +// - "Acute angle" for angles greater than 0° and less than 90° |
| 180 | +// - "Right angle" for exactly 90° |
| 181 | +// - "Obtuse angle" for angles greater than 90° and less than 180° |
| 182 | +// - "Straight angle" for exactly 180° |
| 183 | +// - "Reflex angle" for angles greater than 180° and less than 360° |
| 184 | +// - "Invalid angle" for angles outside the valid range. |
| 185 | + |
| 186 | +// Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.) |
| 187 | + |
| 188 | +// Acceptance criteria: |
| 189 | +// After you have implemented the function, write tests to cover all the cases, and |
| 190 | +// execute the code to ensure all tests pass. |
| 191 | + |
| 192 | +function getAngleType(angle) { |
| 193 | + // Check for invalid angles (outside 0-360 range) |
| 194 | + if (angle <= 0 || angle >= 360) { |
| 195 | + return "Invalid angle"; |
| 196 | + } |
| 197 | + |
| 198 | + // Check for specific angle types |
| 199 | + if (angle > 0 && angle < 90) { |
| 200 | + return "Acute angle"; |
| 201 | + } else if (angle === 90) { |
| 202 | + return "Right angle"; |
| 203 | + } else if (angle > 90 && angle < 180) { |
| 204 | + return "Obtuse angle"; |
| 205 | + } else if (angle === 180) { |
| 206 | + return "Straight angle"; |
| 207 | + } else if (angle > 180 && angle < 360) { |
| 208 | + return "Reflex angle"; |
| 209 | + } |
| 210 | + |
| 211 | + // This line should theoretically never be reached given our conditions, |
| 212 | + // but included as a safety net |
| 213 | + return "Invalid angle"; |
| 214 | +} |
| 215 | + |
| 216 | +// The line below allows us to load the getAngleType function into tests in other files. |
| 217 | +// This will be useful in the "rewrite tests with jest" step. |
| 218 | +module.exports = getAngleType; |
| 219 | + |
| 220 | +// This helper function is written to make our assertions easier to read. |
| 221 | +// If the actual output matches the target output, the test will pass |
| 222 | +function assertEquals(actualOutput, targetOutput) { |
| 223 | + console.assert( |
| 224 | + actualOutput === targetOutput, |
| 225 | + `Expected ${actualOutput} to equal ${targetOutput}` |
| 226 | + ); |
| 227 | +} |
| 228 | + |
| 229 | +// Write tests to cover all cases, including boundary and invalid cases. |
| 230 | +console.log("Running tests for getAngleType function...\n"); |
| 231 | + |
| 232 | +// Test 1: Acute angles |
| 233 | +console.log("Testing Acute angles:"); |
| 234 | +const acute1 = getAngleType(45); |
| 235 | +assertEquals(acute1, "Acute angle"); |
| 236 | + |
| 237 | +const acute2 = getAngleType(89); |
| 238 | +assertEquals(acute2, "Acute angle"); |
| 239 | + |
| 240 | +const acute3 = getAngleType(1); |
| 241 | +assertEquals(acute3, "Acute angle"); |
| 242 | + |
| 243 | +// Test 2: Right angle |
| 244 | +console.log("\nTesting Right angle:"); |
| 245 | +const right = getAngleType(90); |
| 246 | +assertEquals(right, "Right angle"); |
| 247 | + |
| 248 | +// Test 3: Obtuse angles |
| 249 | +console.log("\nTesting Obtuse angles:"); |
| 250 | +const obtuse1 = getAngleType(95); |
| 251 | +assertEquals(obtuse1, "Obtuse angle"); |
| 252 | + |
| 253 | +const obtuse2 = getAngleType(135); |
| 254 | +assertEquals(obtuse2, "Obtuse angle"); |
| 255 | + |
| 256 | +const obtuse3 = getAngleType(179); |
| 257 | +assertEquals(obtuse3, "Obtuse angle"); |
| 258 | + |
| 259 | +// Test 4: Straight angle |
| 260 | +console.log("\nTesting Straight angle:"); |
| 261 | +const straight = getAngleType(180); |
| 262 | +assertEquals(straight, "Straight angle"); |
| 263 | + |
| 264 | +// Test 5: Reflex angles |
| 265 | +console.log("\nTesting Reflex angles:"); |
| 266 | +const reflex1 = getAngleType(185); |
| 267 | +assertEquals(reflex1, "Reflex angle"); |
| 268 | + |
| 269 | +const reflex2 = getAngleType(270); |
| 270 | +assertEquals(reflex2, "Reflex angle"); |
| 271 | + |
| 272 | +const reflex3 = getAngleType(359); |
| 273 | +assertEquals(reflex3, "Reflex angle"); |
| 274 | + |
| 275 | +// Test 6: Invalid angles (boundary cases and outside range) |
| 276 | +console.log("\nTesting Invalid angles:"); |
| 277 | + |
| 278 | +// Boundary cases at 0 and 360 |
| 279 | +const invalid1 = getAngleType(0); |
| 280 | +assertEquals(invalid1, "Invalid angle"); |
| 281 | + |
| 282 | +const invalid2 = getAngleType(360); |
| 283 | +assertEquals(invalid2, "Invalid angle"); |
| 284 | + |
| 285 | +// Negative angles |
| 286 | +const invalid3 = getAngleType(-45); |
| 287 | +assertEquals(invalid3, "Invalid angle"); |
| 288 | + |
| 289 | +const invalid4 = getAngleType(-90); |
| 290 | +assertEquals(invalid4, "Invalid angle"); |
| 291 | + |
| 292 | +const invalid5 = getAngleType(-180); |
| 293 | +assertEquals(invalid5, "Invalid angle"); |
| 294 | + |
| 295 | +// Angles greater than 360 |
| 296 | +const invalid6 = getAngleType(365); |
| 297 | +assertEquals(invalid6, "Invalid angle"); |
| 298 | + |
| 299 | +const invalid7 = getAngleType(400); |
| 300 | +assertEquals(invalid7, "Invalid angle"); |
| 301 | + |
| 302 | +const invalid8 = getAngleType(720); |
| 303 | +assertEquals(invalid8, "Invalid angle"); |
| 304 | + |
| 305 | +console.log("\nAll tests completed!"); |
| 306 | + |
0 commit comments