# A function that takes in a function Y and a number, and returns a function which when called, invokes Y some number of times

**URL:** <https://discourse.processing.org/t/a-function-that-takes-in-a-function-y-and-a-number-and-returns-a-function-which-when-called-invokes-y-some-number-of-times/22512>\
**Category:** Coding Questions\
**Created:** [July 9, 2020, 4:02pm UTC](https://discourse.processing.org/t/a-function-that-takes-in-a-function-y-and-a-number-and-returns-a-function-which-when-called-invokes-y-some-number-of-times/22512 "2020-07-09T16:02:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Biggy](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@Biggy](https://discourse.processing.org/u/Biggy)\
**Post date:** [July 9, 2020, 4:02pm UTC](https://discourse.processing.org/t/a-function-that-takes-in-a-function-y-and-a-number-and-returns-a-function-which-when-called-invokes-y-some-number-of-times/22512/1 "2020-07-09T16:02:56Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

```auto
function loopable(n, func) {
			return function (...args) {
				function looper() {
					if (n === 0) {
						return undefined;
					}
					n -= 1;
					func(...args);
					looper();
				}
				looper();
			};
		}

```

It works as expected in dev console, when I pass it console.log and some number, however it fails for other functions that draw stuff. What’s wrong with it?

usage:

```auto
var writeString_loopable = loopable(n, console.log);
writeString_loopable('hello');

```

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [July 13, 2020, 6:06am UTC](https://discourse.processing.org/t/a-function-that-takes-in-a-function-y-and-a-number-and-returns-a-function-which-when-called-invokes-y-some-number-of-times/22512/2 "2020-07-13T06:06:17Z")

</div>

This question is not related to p5.js but to javascript. My suggestion to you is to write readable code. Instead of going fancy with `...args`, replace that and explicitly define the variables used in your function.

What error do you get when you run your code?

Kf

---

<div class="post-metadata">

**Author:** ![furinax](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/furinax/32/9699_2.png) [@furinax](https://discourse.processing.org/u/furinax)\
**Post date:** [July 14, 2020, 5:02am UTC](https://discourse.processing.org/t/a-function-that-takes-in-a-function-y-and-a-number-and-returns-a-function-which-when-called-invokes-y-some-number-of-times/22512/3 "2020-07-14T05:02:17Z")

</div>

Why are you calling looper within looper? In fact why use recursion in the first place? Couldn’t you just use a simple iterative for loop? The n should be stored as a closure variable so you could simply reference it in the for loop.

You should also probably return a value when calling looper, unless the caller doesn’t care about it.

---

<div class="post-metadata">

**Author:** ![Biggy](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@Biggy](https://discourse.processing.org/u/Biggy)\
**Post date:** [July 14, 2020, 9:26am UTC](https://discourse.processing.org/t/a-function-that-takes-in-a-function-y-and-a-number-and-returns-a-function-which-when-called-invokes-y-some-number-of-times/22512/4 "2020-07-14T09:26:19Z")

</div>

The error is ‘illegal invocation’ and apparently it’s specific to canvas functions. When I pass `ctx.fillRect` as an argument to `loopable`, `this` is not bound to `ctx`. The fix was to use the `bind` function.

---

<div class="post-metadata">

**Author:** ![Biggy](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@Biggy](https://discourse.processing.org/u/Biggy)\
**Post date:** [July 14, 2020, 10:25am UTC](https://discourse.processing.org/t/a-function-that-takes-in-a-function-y-and-a-number-and-returns-a-function-which-when-called-invokes-y-some-number-of-times/22512/5 "2020-07-14T10:25:59Z")

</div>

In attempting to learn JS, I’m trying alternatives to conventional stuff like loops. How would you improve this particular code?

---

<div class="post-metadata">

**Author:** ![furinax](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/furinax/32/9699_2.png) [@furinax](https://discourse.processing.org/u/furinax)\
**Post date:** [July 15, 2020, 1:02am UTC](https://discourse.processing.org/t/a-function-that-takes-in-a-function-y-and-a-number-and-returns-a-function-which-when-called-invokes-y-some-number-of-times/22512/6 "2020-07-15T01:02:14Z")

</div>

I haven’t done Cinder on js yet but I would think using arrow functions would be a start.
