The challenge
Write makeCounter(start, step) that returns a function. Each time that function is called, it returns the next number: first start + step, then start + 2 × step, and so on. Each counter must be independent.
const byTwo = makeCounter(0, 2);
byTwo(); // 2
byTwo(); // 4
Try it
Write your answer below, then press Check my answer. Your code runs right here in your browser, and the tests tell you what works.
It prints
What the tests check
- Counting by 2 from 0 gives 2, then 4:
(() => { const c = makeCounter(0, 2); c(); return c(); })()gives4 - Counting down from 10 gives 9:
(() => { const c = makeCounter(10, -1); return c(); })()gives9 - Counters don’t share:
(() => { const a = makeCounter(0, 1); const b = makeCounter(0, 1); a(); a(); return b(); })()gives1
Hints
Stuck? Open one hint at a time. Try again after each one.
Hint 1
Keep a let current = start; inside makeCounter, and return a function that changes it.
Hint 2
Each call to makeCounter creates a fresh current, so counters are independent.
A solution
There’s always more than one right way. Read it, make sure you understand it, then try writing it yourself without looking.
Show one possible solution
function makeCounter(start, step) {
let current = start;
return () => {
current += step;
return current;
};
}
Learn what you need
This exercise uses what Grade 9: Scope, Hoisting & Closures teaches. Read those notes first if any of it is new.