Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Count Truthy Values

Fundamentals: Part 1medium

Course · Section 2: JavaScript Fundamentals – Part 1 · Lecture 11: Data Types

Implement `countTruthy(arr)` returning how many elements of `arr` are truthy.

Sample tests

Input: countTruthy([0,1,2,"",null,"a"])
Output: 3
Input: countTruthy([false,0,""])
Output: 0

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Counting with a manual loop and == comparisons invites coercion bugs; Boolean is clearer.

Learning resources

  • MDN: typeof
Approach & explanation (try first)

filter(Boolean) drops every falsy element, so its length is the number of truthy values.

Loading...
⌘/Ctrl + Enter

Run your code to see results.