Skip to main content
File

title: times tags: function,intermediate

TS JS Deno

Iterates over a callback n times

Use Function.call() to call fn n times or until it returns false. Omit the last argument, context, to use an undefined object (or the global object in non-strict mode).

const times = (n: number, fn: Function, context: any = undefined) => {
  let i = 0;
  while (fn.call(context, i) !== false && ++i < n) {}
};
var output = "";
times(5, (i) => (output += i));
console.log(output); // 01234

var output = "";
const result = times(5, (i: string) => (output += i));
assertEquals(output, "01234");
assertEquals(result, "01234");