I need help to understand how const colorIntervals
can receive two arguments as a callback (as it shows at the end).
The task is to implement the function colorFence(length)
, where length
is the total length of the fence. This function should return another function which can be called with arguments left
and right
, where 0
≤ left
≤ right
< length
. The returned function colors the segment of the fence from left to right (inclusive) into the black color, and returns the remaining total length of the white fence.
An example of interaction is as follows:
const colorIntervals = colorFence(20);
colorIntervals(18, 18); // The remaining length of the white fence is 19.
colorIntervals(0, 3); // The remaining length of the white fence is 15.
colorIntervals(17, 19); // The remaining length of the white fence is 13 (since segment 18 was already painted).
colorIntervals(0, 19); // The remaining length of the white fence is 0.