Implement debounce, then make it cancellable
Easy20 minFree, no account
The classic warm-up, and the follow-up that catches people who learned it by heart.
The question
Write debounce(fn, wait): the returned function delays calling fn until wait milliseconds have passed without another call.
Then add .cancel() to drop a pending call, and .flush() to run it immediately.
function debounce(fn, wait) {
// returns a function with .cancel() and .flush()
}
const save = debounce(write, 300);
save(1); save(2); save(3); // write(3) runs once, 300ms after the last call20:00Commit to an answer before you open the solution. Reading it first teaches you to recognise good answers, which is not the skill being tested.
Stuck?
0 of 3 hints takenThe worked solution
written by a person · not a gradeScore yourself
0 of 4 marked- Working debounce with correct timer reset30
- cancel and flush behave correctly, including flush with nothing pending30
- Preserved
thisand arguments, and said why25 - Stated complexity and named the leading-edge or throttle variant15
We run no AI here and nothing on this page grades you. The score is yours, and the useful number is the one you get on the same problem a month from now, cold.
kept in this browser only