0

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 0leftright < 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.
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Pipi
  • 11
  • 1

1 Answers1

0

Your question precisely describes what you need to do:

  1. create a function colorFence
  • arguments: length
  • return: another function
  1. create another function
  • arguments: left, right
  • return 0 <= left <= right < left

Below you find a pseudo code skeleton. Try to adapt this requirements into the pseudo code.

function first(a) {
  console.log("first", a)
  return second
}

function second(b, c) {
  console.log("second", b, c)
}

const firstFunctionExecution = first(1)
firstFunctionExecution(2, 3)
firstFunctionExecution(4, 5)
Jonathan Stellwag
  • 3,843
  • 4
  • 25
  • 50