Full-Stack course / JavaScript fundamentals
Status code family
How to fetch and submit this exercise
Once: install the tool and connect your account.
curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon loginEach exercise: download it, edit the file named in the instructions, then submit.
gonomicon fetch fs-week-2/01-06-status-code-family
cd 01-06-status-code-family
gonomicon submitStatus code family
Goal: sort an HTTP status code into its family, like the ones you met in the HTTP lesson, and call anything that isn't a real status code "unknown".
Worked example
The file starts with let status = ...;. Your code creates a let box called family and fills it with one of six texts.
status | family |
|---|---|
200 | "success" |
301 | "redirect" |
404 | "client error" |
503 | "server error" |
101 | "informational" |
600 | "unknown" |
"404" | "unknown" |
The families
| Range | family |
|---|---|
| 100 to 199 | "informational" |
| 200 to 299 | "success" |
| 300 to 399 | "redirect" |
| 400 to 499 | "client error" |
| 500 to 599 | "server error" |
| anything else | "unknown" |
"Anything else" includes numbers outside 100 to 599, decimals like 200.5, text (even text that looks like a number), NaN, null, undefined and booleans. A real status code is a whole number from 100 to 599.
Rules
- Keep the line
let status = ...;at the top of the file. The tests replace the value on it. - Declare a
letbox calledfamily. It must end up as exactly one of the six texts above. - Use a
switchstatement on the hundreds digit of the code. - Don't use
var, and don't use loose==or!=. - To look at your box while you work, add
console.log(family);at the bottom and runnode status_code_family.js.
What you'll practice
- Checking the type and the shape of a value before trusting it
iffor ranges andswitchfor exact choices, together- Using
%to pull a number apart - Strict equality with mixed types
Run it
gonomicon fetch fs-week-2/01-06-status-code-family
cd 01-06-status-code-family
gonomicon test
gonomicon submit
Hints
- Two jobs, in order: first decide whether
statusis a real status code at all (a number, whole, from 100 to 599). Only then work out its family. - Start with
let family = "unknown";. Then, only if the code is valid, get the hundreds digit: take away the remainder with%, then divide by 100. 404 becomes 4. - Wrap it up as
if (valid) { switch (hundreds) { case 1: family = "informational"; break; ... } }. Eachcaseis a digit, and every one needs itsbreak. Check validity withtypeof status === "number", the range, andstatus % 1 === 0for whole.