Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

Conditional Class

Props & Classeseasy

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

Implement `conditionalClass(base, condition, extra)` returning `base` plus a space and `extra` when `condition` is true, otherwise just `base`.

Sample tests

Input: conditionalClass("btn", true, "active")
Output: "btn active"
Input: conditionalClass("btn", false, "active")
Output: "btn"

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Adding a leading or trailing space when extra is empty.

Learning resources

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

A small helper that conditionally appends a modifier class, the simplest form of conditional styling.

Loading...
⌘/Ctrl + Enter

Run your code to see results.