Full-Stack course / JavaScript fundamentals
Truthy tally
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-05-truthy-tally
cd 01-05-truthy-tally
gonomicon submitTruthy 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];:
| Value | Truthy? | Adds to pattern |
|---|---|---|
0 | no | N |
"hello" | yes | Y |
"" | no | N |
42 | yes | Y |
null | no | N |
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. truthyCountis a number.patternis a string with exactly oneYorNper value, in order.- Walk through the list with a
for...ofloop. - 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 runnode truthy_tally.js.
What you'll practice
- Which values pass the truthy gate and which are stopped
for...ofover a list- Building up a count and a string at the same time
- Choosing a track with
ifandelse
Run it
gonomicon fetch fs-week-2/01-05-truthy-tally
cd 01-05-truthy-tally
gonomicon test
gonomicon submit
Hints
- You don't need to compare each value with
0,""ornullone by one. Anifalready asks the truthy gate about whatever you give it. - Make two
letboxes 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. - Loop with
for (const value of values). Inside,if (value)is the track where you add 1 totruthyCountand a "Y" topattern; theelsetrack adds an "N".