Full-Stack course / JavaScript fundamentals
Describe the box
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-02-describe-the-box
cd 01-02-describe-the-box
gonomicon submitDescribe the box
Goal: look at the value in a box and say, in plain words, what kind of thing it is.
Worked example
The file starts with a box called value. The tests swap in different values, run your code, and read the kind box you filled in.
What value holds | kind should end up as |
|---|---|
42 | "number" |
"42" | "text" |
false | "yes/no" |
undefined (a box made with let value; and never packed) | "empty" |
null | "nothing" |
NaN (what you get from Number("abc")) | "not a number" |
Notice that 42 and "42" look alike but get different answers, and that undefined and null do too.
Rules
- Keep the line
let value = ...;at the top of the file. The tests replace it with other values. - Your code goes below it and must leave one of the six exact words above in
kind. Use anif,else if,elsechain or aswitchto choose. - Compare with
===, not==. - Don't use
var. - To look at your result while you work, add
console.log(kind);at the bottom and runnode describe_the_box.js.
What you'll practice
- Telling the shapes of values apart, including the odd ones
- Why strict equality matters
- Choosing between tracks in the right order
Run it
gonomicon fetch fs-week-2/01-02-describe-the-box
cd 01-02-describe-the-box
gonomicon test
gonomicon submit
Hints
- Add
console.log(typeof value);and try several different values. Which shapes give you a word you can use as it is, and which give you something you don't want? - Two values are tricky: NaN reports itself as a number, and null does not report itself as "null". Check for those special values first, before you ask about the shape.
- Check
value === nullfirst, thenvalue === undefined, thenNumber.isNaN(value). After that,typeof valuetells you number, string or boolean, and each one maps to a word in the list.