Skip to curriculum
Jump to the editor

6.7. Toggle through a list of values

Easy
Topics

This question is in

6. Functions and closures
15–20 min

A theme switcher goes light, dark, system and back to light. A video player's speed button steps through 1×, 1.5× and 2×, and a settings page can jump straight to one of them. The toggle has to remember where it is, move both ways, wrap at both ends, and never be thrown off by code that changes the array it was made from.

Write createToggle(values) in toggle.js. It returns an object with four methods that move through a private copy of values. The toggle starts on the first value.

Signature

JAVASCRIPT
function createToggle(values) // returns { current, next, previous, set }
  • values: a non-empty array of any values.
  • current(): returns the current value without moving.
  • next(): moves one step forward, from the last value to the first, and returns the new current value.
  • previous(): moves one step back, from the first value to the last, and returns the new current value.
  • set(value): moves to the first position that holds value and returns it. NaN finds NaN; objects are found only by identity.

Requirements

  1. createToggle returns an object whose current, next, previous and set are functions; the toggle starts on the first value, and current never moves it.
  2. next moves forward and wraps from the last value to the first; previous moves back and wraps from the first to the last. Both return the new value.
  3. With one value, every move stays on it.
  4. set(value) jumps to the first position holding value and returns it, and later moves go on from there. A value not in the list throws a RangeError and leaves the position as it was.
  5. The toggle keeps its own copy: changing the original array afterwards does not change the toggle, and the toggle never changes the original. A hole in the array counts as undefined.
  6. values that is not an array throws a TypeError; an empty array throws a RangeError.
  7. The methods work when taken off the object, and toggles are independent.

Examples

JAVASCRIPT
const theme = createToggle(["light", "dark", "system"]);
theme.current();  // "light"
theme.next();     // "dark"
theme.next();     // "system"
theme.next();     // "light"
theme.previous(); // "system"
JAVASCRIPT
const speed = createToggle([1, 1.5, 2]);
speed.set(2);  // 2
speed.next();  // 1
speed.set(3);  // throws RangeError; still on 1
JAVASCRIPT
const list = ["a", "b"];
const toggle = createToggle(list);
list.push("c");
toggle.previous(); // "b": the toggle kept its own copy

Environment

  • Plain JavaScript in a browser page; the tests call the global createToggle from toggle.js.
  • Out of scope: listeners for changes, adding or removing values later.

Hints

  1. What is -1 % 3 in JavaScript? What does that mean for previous on the first value?
  2. Which array method finds NaN, and which one does not?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…