Full-Stack course / JavaScript fundamentals
Arrow toolbox
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/02-02-arrow-toolbox
cd 02-02-arrow-toolbox
gonomicon submitArrow toolbox
Goal: build three small tools as arrow functions, each one returning a value made from its inputs.
Worked example
| Call | Returns |
|---|---|
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 backtruewhennis at leastlowand at mosthigh, andfalseotherwise. Both ends count as "between". Iflowis bigger thanhigh, 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 thoughcountis 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
functionanywhere in the file. - Each one must
returnits 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 runnode arrow_toolbox.js.
What you'll practice
- Writing arrow functions in both of their natural shapes
- Combining comparisons with
&&to get atrueorfalse - 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
- An arrow function is
const name = (inputs) => result;. When the work is one expression, there are no curly braces and noreturn: whatever is after the arrow comes back automatically. isBetweenhas to be true only when two things are both true:nis at leastlowandnis at mosthigh. Join two comparisons with&&.averageadds the two numbers and then divides, so mind the parentheses.labelis a template literal with two blanks.const isBetween = (n, low, high) => n >= low && n <= high;const average = (a, b) => (a + b) / 2;and forlabel, write the name, a space, then the count in round brackets: use backticks with${name}and${count}for the blanks.