-2

While searching for arrow functions I came across this example

let labels = [];
repeat(5, i => {
  labels.push(`Unit ${i + 1}`);
});
console.log(labels);
// → ["Unit 1", "Unit 2", "Unit 3", "Unit 4", "Unit 5"]

1st, repeat method from MDN seems to accept only one parameter (count).

2nd, arrow functions should be written like this: i = () => {}, and not i => {}

Is this an out of date snippet that doesn't work anyway or should I interpret it in another way?

leonardofed
  • 936
  • 2
  • 9
  • 24
  • the repeat function here is written by the user , and not the string method , and the arrow function syntax is correct , take a look https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions – mostafa tourad Jun 24 '18 at 09:50
  • Only when the function doesn't expect any parameters, should you use `() =>` – haim770 Jun 24 '18 at 09:50

3 Answers3

1

The repeat method mentioned in the article is not String.prototype.repeat, but a standalone function defined in the article itself - it's not a built-in function:

But what if we want to do something other than logging the numbers? Since “doing something” can be represented as a function and functions are just values, we can pass our action as a function value.

function repeat(n, action) {
  for (let i = 0; i < n; i++) {
    action(i);
  }
}
repeat(3, console.log);

The syntax you mention

arrow functions should be written like this: i = () => {}, and not i => {}

is only true if the arrow function in question is meant to accept exactly zero parameters. An arrow function with one parameter can have parentheses omitted from the parameter list. (Two or more parameters require parentheses again)

The snippet you mentioned does indeed work fine:

function repeat(n, action) {
  for (let i = 0; i < n; i++) {
    action(i);
  }
}

let labels = [];
repeat(5, i => {
  labels.push(`Unit ${i + 1}`);
});
console.log(labels);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

It looks like an own function of repeat, not one of String.

In this case, you can pass a function with the wanted signature of arguments to the function.

const
    repeat = (l, fn) => {
        var i;
        for (i = 0; i < l; i++) fn(i);
    };

let labels = [];

repeat(5, i => labels.push(`Unit ${i + 1}`));

console.log(labels); // → ["Unit 1", "Unit 2", "Unit 3", "Unit 4", "Unit 5"]
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Thanks, good answer. I somehow totally misread the repeat func. I'd probably replace the declaration of "i" outside the loop and use a let. :) – leonardofed Jun 24 '18 at 15:57
0
  1. First thing first as you said the repeat built-in function only accept one parameter, so the repeat function used here is not built-in it is user-defined function

  2. The arrow function can be written as i = () => {} exactly but here it is an anonymous arrow function which means it doesn't require a name, we can also write the repeat function like this:

    repeat(5, function(i) { labels.push(i) })

Arrow function looks readable, that's why it has been written like that.

  1. If you're confused about the statement inside the for-loop, so it is that repeat function which is called inside the for-loop

    Maybe it will help.

Imad_K
  • 1
  • 3