Course outline
Full-Stack course / JavaScript fundamentals

Status code family

Lab

How to fetch and submit this exercise

Once: install the tool and connect your account.

curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon login

Each 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 submit

The runner depends on the exercise: JavaScript/TypeScript/React labs run with Vitest, Python labs with pytest, and config-file labs (Docker, CI, Terraform, Nginx) are checked by parsing your file. gonomicon test runs the visible checks on your computer first; submitting also runs the hidden checks on our server.

Status 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.

statusfamily
200"success"
301"redirect"
404"client error"
503"server error"
101"informational"
600"unknown"
"404""unknown"

The families

Rangefamily
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 let box called family. It must end up as exactly one of the six texts above.
  • Use a switch statement 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 run node status_code_family.js.

What you'll practice

  • Checking the type and the shape of a value before trusting it
  • if for ranges and switch for 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

  1. Two jobs, in order: first decide whether status is a real status code at all (a number, whole, from 100 to 599). Only then work out its family.
  2. 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.
  3. Wrap it up as if (valid) { switch (hundreds) { case 1: family = "informational"; break; ... } }. Each case is a digit, and every one needs its break. Check validity with typeof status === "number", the range, and status % 1 === 0 for whole.