javascript - Recursion, pass function as an argument - Stack Overflow

I want to run a function certain amount of times using recursion, for example: repeat(console.log('

I want to run a function certain amount of times using recursion, for example: repeat(console.log('Hello'), 3) should print Hello 3 times. I tried to implement such function but it prints only one word Hello.

function repeat(w, x){
        let fun = function(arg){
            return arg
        }
        if (x==1){
            return fun(w)
          }
          else{
            return fun(w)+repeat(fun(w), x-1)
        }
    }
  repeat(console.log('Hello'), 3)

I want to run a function certain amount of times using recursion, for example: repeat(console.log('Hello'), 3) should print Hello 3 times. I tried to implement such function but it prints only one word Hello.

function repeat(w, x){
        let fun = function(arg){
            return arg
        }
        if (x==1){
            return fun(w)
          }
          else{
            return fun(w)+repeat(fun(w), x-1)
        }
    }
  repeat(console.log('Hello'), 3)

Share Improve this question edited Nov 14, 2020 at 15:50 CodeBug 1,6671 gold badge10 silver badges23 bronze badges asked Nov 14, 2020 at 15:00 JohnPixJohnPix 1,8531 gold badge26 silver badges55 bronze badges 2
  • Could you add an example on how you would call this function, initially ? – Nicolas Commented Nov 14, 2020 at 15:03
  • @Nicolas He did in the text. – Barmar Commented Nov 14, 2020 at 15:03
Add a ment  | 

5 Answers 5

Reset to default 5

You're not passing the function as an argument. You're calling the function and passing its return value.

Just pass fun with no argument list. In the initial call, you can use an anonymous function.

function repeat(fun, x) {
  if (x == 1) {
    return fun()
  } else {
    return fun() + repeat(fun, x - 1)
  }
}

repeat(() => console.log("Hello"), 3);
console.log(repeat((a = 0) => a + 1, 5));

A few mistakes in your code:

  1. You're not passing the function as an argument.
  2. You don't need to use + to make two functions run after each other. From your original snippet I infer that you're only concerned about the side effects, not the values. So you can just call the functions individually.

It could look something like this:

  function repeat(fun, x) {
   if(x < 1) return; // we ran out of attempts, terminate
   fun(); // side-effect (print to console)
   repeat(fun, x - 1); // decrement the counter and attempt to continue
}
    
repeat(function() { console.log("hello")}, 3);

This code has a recursive function that accept a function and number of times to execute it.

function repeat(func, x){
    if(x === 0) return;
    func(x);
    repeat(func, x - 1);
}

repeat((x) => {console.log(`Hello ${x}`)}, 3);

You need pass a function and maybe use a iteraror like for(){} to repeat x times

function repeat(w, x){
    for (let i = 0; i < x; i++) {
        w.call()
    }
}

repeat(()=>console.log("Foo"),4)

When calling the function repeat(console.log('Hello'), 3), you are assiging the returns value of the function console.log('Hello') as the value of the parameter w. This causes your function to show only one console log, and assigning undefined to the parameters w. What you are looking for might be to pass a function reference as the first parameters, the argument to that function as the second parameters and the number of call as a third parameters.

function repeat(func, arguments, x){
    if (x==1){
        return func.call(this, arguments);
      }
      else{
        return func.call(this, arguments)+repeat(func, arguments, x-1)
    }
}

repeat(console.log, 'Hello', 3);

Note here that i've removed the variable fun and moved it as a parameters.

Also, when you want to pass a reference to a function as an argument, you don't use the parenthesis ( ( ) ). This is what i've done here.

I'm also using the .call function on that first parameters, to call it dynmically. The syntax of the call function goes like this call( thisContext, ...arguments). You can learn more on that function here

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744146134a4560431.html

相关推荐

  • javascript - Recursion, pass function as an argument - Stack Overflow

    I want to run a function certain amount of times using recursion, for example: repeat(console.log('

    8天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信