Toggle with Closure
EasyP3Coding
Implement createToggle(...states), which returns a function. Each call to the returned function returns the next value from states, cycling back to the first after the last. The first call returns the first state.
Every toggle keeps its own position, so two toggles created separately do not affect each other. You will be given at least one state.
Example 1
- Input
- const t = createToggle('on', 'off'); t(); t(); t();
- Output
- 'on', 'off', 'on'
Constraints
- 1 <= states.length <= 10^3
- States can be any values.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 6. ⌘Enter runs, ⌘⇧Enter submits.