Course outline
Full-Stack course / JavaScript fundamentals

Arrow toolbox

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/02-02-arrow-toolbox
cd 02-02-arrow-toolbox
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.

Arrow toolbox

Goal: build three small tools as arrow functions, each one returning a value made from its inputs.

Worked example

CallReturns
isBetween(5, 1, 10)true
isBetween(11, 1, 10)false
isBetween(10, 1, 10)true
average(4, 10)7
average(-2, 3)0.5
label("Tickets", 3)"Tickets (3)"
label("Seats", 0)"Seats (0)"

The three tools

  • isBetween(n, low, high) gives back true when n is at least low and at most high, and false otherwise. Both ends count as "between". If low is bigger than high, nothing can be between them.
  • average(a, b) gives back the mean of two numbers.
  • label(name, count) gives back the text of the name, a space, and the count in round brackets. It always returns text, even though count is a number.

Rules

  • Write each one as an arrow function stored in a const, with exactly these names: isBetween, average, label.
  • Don't use the word function anywhere in the file.
  • Each one must return its value, not print it. With an arrow function and no curly braces the value is returned for you.
  • To try things out while you work, add a line like console.log(isBetween(5, 1, 10)); at the bottom, then remove it, and run node arrow_toolbox.js.

What you'll practice

  • Writing arrow functions in both of their natural shapes
  • Combining comparisons with && to get a true or false
  • Using parentheses to control the order of operators
  • Filling a template literal from two inputs

Run it

gonomicon fetch fs-week-2/02-02-arrow-toolbox
cd 02-02-arrow-toolbox
gonomicon test
gonomicon submit

Hints

  1. An arrow function is const name = (inputs) => result;. When the work is one expression, there are no curly braces and no return: whatever is after the arrow comes back automatically.
  2. isBetween has to be true only when two things are both true: n is at least low and n is at most high. Join two comparisons with &&. average adds the two numbers and then divides, so mind the parentheses. label is a template literal with two blanks.
  3. const isBetween = (n, low, high) => n >= low && n <= high; const average = (a, b) => (a + b) / 2; and for label, write the name, a space, then the count in round brackets: use backticks with ${name} and ${count} for the blanks.