Write throttle and confirm the first call runs immediately.
Acceptance criteria
- 1.#out should show 1.
JavaScript Tutorial · JavaScript Performance
These 10 tasks go with the JavaScript Throttle chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “Throttling limits a function to running at most once in a given time window.” .
1.Build a throttle wrapperexercise · easy · 10 min
Write throttle and confirm the first call runs immediately.
Passes when: #out should show 1.
2.Confirm the first call is immediateexercise · easy · 10 min · practice plan
Read the value from the very first throttled call.
Passes when: #out should show immediate.
3.Drop calls during the intervalexercise · easy · 10 min · practice plan
Fire a hundred rapid calls and confirm only one ran.
Passes when: #out should show 1.
4.Simulate a scroll handlerexercise · easy · 10 min · practice plan
Fire fifty scroll calls and confirm only one layout check ran.
Passes when: #out should show 1.
5.Confirm the wrapper is a functionexercise · medium · 10 min · practice plan
Report the type of a throttled function.
Passes when: #out should show function.
6.Fix throttle that never blocks later callsbug fix · easy · 8 min
Without the ready flag, every call runs immediately.
Passes when: #out should show 1 ran.
7.Fix ready never set back to falsebug fix · easy · 8 min · practice plan
Without setting ready to false, the second call also gets through.
Passes when: #out should show 1.
8.Fix debounce used where throttle was neededbug fix · easy · 8 min · practice plan
Debounce would delay the first call; throttle should run it immediately.
Passes when: #out should show immediate.
9.Fix the arguments never forwardedbug fix · easy · 8 min · practice plan
The wrapped function should receive its arguments.
Passes when: #out should show test.
10.Fix the count assumed to be zerobug fix · easy · 8 min · practice plan
Throttle always lets the first call through, unlike debounce.
Passes when: #out should show 1.
Between them these tasks cover throttle, Rate limiting and Immediate execution.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.