Skip to content

cycle

The cycle function repeats the elements of an array or characters of a string a specified number of times.

Syntax

typescript
cycle<T>(array: T[], times: number): T[];
cycle(string: string, times: number): string;

Parameters

NameTypeDescription
arrayT[] | stringSource array or string
timesnumberNumber of repetitions

Returns

TypeDescription
T[] | stringNew repeated array or string

Examples

typescript
cycle([1, 2], 3); // => [1,2,1,2,1,2]
cycle("ab", 2); // => "abab"

Notes

  • If times is 0 or negative, returns an empty array or string.
  • Does not modify the original array or string.

References

Released under the MIT License.