Course outline
Full-Stack course / JavaScript fundamentals

Truthy tally

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-05-truthy-tally
cd 01-05-truthy-tally
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.

Truthy tally

Goal: go through a list of mixed values, count how many pass the truthy gate, and record a Y or N for each one.

Worked example

The file starts with a list of five values in a const box called values. Your code creates two let boxes: truthyCount and pattern.

With const values = [0, "hello", "", 42, null];:

ValueTruthy?Adds to pattern
0noN
"hello"yesY
""noN
42yesY
nullnoN

So truthyCount is 2 and pattern is "NYNYN": one letter per value, in the same order as the list.

Rules

  • Keep the line const values = [...]; at the top of the file. The tests replace the values on it.
  • truthyCount is a number. pattern is a string with exactly one Y or N per value, in order.
  • Walk through the list with a for...of loop.
  • Your code should not care how many values the list holds. Most tests use five, but some use more, fewer, or none at all.
  • Use the language's own idea of truthy and falsy. Don't compare against each falsy value yourself.
  • Don't use var.
  • To look at your boxes while you work, add console.log(truthyCount, pattern); at the bottom and run node truthy_tally.js.

What you'll practice

  • Which values pass the truthy gate and which are stopped
  • for...of over a list
  • Building up a count and a string at the same time
  • Choosing a track with if and else

Run it

gonomicon fetch fs-week-2/01-05-truthy-tally
cd 01-05-truthy-tally
gonomicon test
gonomicon submit

Hints

  1. You don't need to compare each value with 0, "" or null one by one. An if already asks the truthy gate about whatever you give it.
  2. Make two let boxes before the loop: a counter that starts at 0, and a text box that starts as the empty string. Every lap adds one letter to the text box, and some laps also add 1 to the counter.
  3. Loop with for (const value of values). Inside, if (value) is the track where you add 1 to truthyCount and a "Y" to pattern; the else track adds an "N".