Closures and a sequence generator with memory
JavaScript
Hard
closures
Task «Closures and a sequence generator with memory»
Implement the function `createSequenceGenerator`, which takes an initial value and a step. The function returns an object with two methods: `next()` and `reset()`. The `next()` method returns the next value of the sequence (starting from the initial value, increasing by the step on each call). The `reset()` method resets the sequence to the initial value. Additionally, the `next()` method must remember all generated values in an array, accessible via the `history` property of the object. The `history` property must be protected from direct external modification, but must be readable. Use closures to encapsulate the state.
Input Format
First line: initial value (integer). Second line: step (integer). Next line with commands: 'next' (call next()) or 'reset' (call reset()). Commands are repeated until the end of input.
Output Format
For each 'next' command, output the current value of the sequence. For 'reset', output nothing.
Examples
Example 1
INPUT
10
-3
next
next
next
reset
next
OUTPUT
10
7
4
10
Example 2
INPUT
2
7 13
OUTPUT
91
main.js