Classify an HTTP status code
beginner Automation utilities Challenge 18 of 18
Write statusClass(code), a small helper for API tests that tells you which class an HTTP status code belongs to.
Requirements
- Return
'informational'for 100–199,'success'for 200–299,'redirection'for 300–399,'client error'for 400–499 and'server error'for 500–599. - Throw a
RangeErrorwhencodeis not an integer (for example404.5,NaNor the string'404'). - Throw a
RangeErrorwhencodeis below 100 or above 599.
Expected output
statusClass(204) → 'success' statusClass(404) → 'client error' statusClass(503) → 'server error' statusClass(600) → throws RangeError
Test cases (6)
- ○classifies 1xx and 2xx codes, including boundaries
- ○classifies 3xx and 4xx codes
- ○classifies 5xx codes up to 599
- ○throws a RangeError just outside the range (99 and 600)
- ○throws a RangeError for non-integers
- ○throws for zero, negative numbers and numeric strings
solution.jsExecutes in your browser
Ctrl/⌘ + Enter to run tests · your code is saved in this browser
Results
Run the tests to see results here.
Related interview questions
- Compare Promise.all, Promise.allSettled, Promise.race and Promise.any. When would you use each in test code?
- What is a closure? Show a practical use in test code.
- Explain the event loop, and why await inside forEach does not wait.
- Write a retry helper with exponential backoff for a flaky async operation.