Skip to main content

Practice ยท 1 of 1

Build a Counter

1. Write a function makeCounter() that returns an object with two methods: increment() (adds 1) and value() (returns the current count). The count must start at 0 and be remembered between calls โ€” a local variable inside makeCounter is the right home for it. 2. Create const counter = makeCounter();, call counter.increment() twice, then log counter.value() โ€” it should print 2.

Difficulty: intermediate

Back to lesson: Practice: Scope and Arrow Functions โ€” Practice