Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

Join Truthy Class Args

Props & Classesmedium

Course · Section 9: Data Structures, Modern Operators and Strings · Lecture 111: The Spread Operator (...)

Implement `cx(...names)` returning a space-separated string of the truthy string arguments, ignoring falsy ones.

Sample tests

Input: cx("btn", false, "primary", null, "lg")
Output: "btn primary lg"
Input: cx("only")
Output: "only"

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Letting false, null, or empty strings slip into the output.
  • Using join without filtering, producing double spaces.

Learning resources

  • React: Passing Props to a Component
Approach & explanation (try first)

names.filter(Boolean) removes every falsy argument so conditional classes like cond && 'active' disappear cleanly, then join adds the spaces.

Loading...
⌘/Ctrl + Enter

Run your code to see results.