Proof of Work · Challenge
The first call runs. The rest wait their turn.
about 25 min · 6 required tests · in the browser
What is recorded: when you save, your Result page shows your code, which tests passed, how many times you ran the tests, how many versions you tried, the time from first run to save, and the largest jump in code between runs. Anyone with the link can see it. The suggested time is not enforced.
A scroll handler should react at once, then stay quiet until the next window opens. That is leading-edge throttle. It is not debounce: nothing should fire later on its own.
Implement throttle(fn, wait).
The first call invokes fn immediately and returns whatever fn returned. Further calls during the next wait milliseconds do nothing. They do not invoke fn, and they do not schedule a later call. After wait ms have passed since the last invocation, the next call invokes fn again.
With wait of 100:
const t = throttle((n) => n * 2, 100)
t(21) // 42, immediately
t(99) // ignored: still inside the window
// 100ms pass: fn does not run by itself
t(3) // invokes fn againThe grader moves time on a virtual clock. setTimeout, clearTimeout, and Date.now work. Time does not pass on its own, and setInterval is not available.
Requirements:
fn immediately.fn.wait ms have passed since the last invocation, the next call invokes fn again.fn receives the arguments of the call that actually invoked it.fn, return what fn returned.Write a top-level function named throttle. Do not use export or module.exports.
Try the contract
This is the behavior the tests check. It is not your code.
Press Esc, then Tab, to leave the editor.
⌘ / Ctrl + Enter runs the tests
Hiring developers? See how teams use this.
Graded on
0/6
Other builds