Skip to content

2757. Generate Circular Array Values 👍

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
function* cycleGenerator(
  arr: number[],
  startIndex: number
): Generator<number, void, number> {
  const n = arr.length;
  let index = startIndex;
  while (true) {
    const jump = yield arr[index];
    index = (index + (jump % n) + n) % n;
  }
}