Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

useToggle Result

Hooks Logiceasy

Course · Section 17: Modern JavaScript Development: Modules, Tooling, and Functional · Lecture 294: Declarative and Functional JavaScript Principles

Implement `useToggle(state, times)` returning the boolean `state` after being toggled `times` times.

Sample tests

Input: useToggle(false, 3)
Output: true
Input: useToggle(false, 2)
Output: false

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Looping and negating times in a row works but is unnecessary; parity is enough.

Learning resources

  • React: Built-in Hooks
Approach & explanation (try first)

A boolean toggled n times equals the original when n is even and its negation when n is odd, so times % 2 decides the result.

Loading...
⌘/Ctrl + Enter

Run your code to see results.