Jump to the editorJAVASCRIPT
JAVASCRIPT JAVASCRIPT JAVASCRIPT
6.7. Toggle through a list of values
Easy
Topics
This question is in
6. Functions and closuresA 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
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 holdsvalueand returns it.NaNfindsNaN; objects are found only by identity.
Requirements
createTogglereturns an object whosecurrent,next,previousandsetare functions; the toggle starts on the first value, andcurrentnever moves it.nextmoves forward and wraps from the last value to the first;previousmoves back and wraps from the first to the last. Both return the new value.- With one value, every move stays on it.
set(value)jumps to the first position holdingvalueand returns it, and later moves go on from there. A value not in the list throws aRangeErrorand leaves the position as it was.- 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. valuesthat is not an array throws aTypeError; an empty array throws aRangeError.- The methods work when taken off the object, and toggles are independent.
Examples
const theme = createToggle(["light", "dark", "system"]);
theme.current(); // "light"
theme.next(); // "dark"
theme.next(); // "system"
theme.next(); // "light"
theme.previous(); // "system"const speed = createToggle([1, 1.5, 2]);
speed.set(2); // 2
speed.next(); // 1
speed.set(3); // throws RangeError; still on 1const list = ["a", "b"];
const toggle = createToggle(list);
list.push("c");
toggle.previous(); // "b": the toggle kept its own copyEnvironment
- Plain JavaScript in a browser page; the tests call the global
createTogglefromtoggle.js. - Out of scope: listeners for changes, adding or removing values later.
Hints
- What is
-1 % 3in JavaScript? What does that mean forpreviouson the first value? - 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…