FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Easy

useCycle

15mins

Implement a function that cycles through a sequence of values each time it is called.

Arguments:

  • The function accepts one or more arguments, each representing a value in the sequence.

Returns:

  • An array with two elements:
    1. value: the current value in the sequence.
    2. cycle: a function that moves to the next value in the sequence.
      If the current value is the last, it cycles back to the first.

Examples:

const [state, cycle] = useCycle("low", "medium", "high"); console.log(state); // "low" cycle(); console.log(state); // "medium" cycle(); console.log(state); // "high" cycle(); console.log(state); // "low"

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests