1

In one of Douglas Crockford speeches, He favours the use of tail recursion over loops. this code was presented,

function repeat(myFunc) {
  if (myFunc !== undefined) {
    return repeat(myFunc);
  }
}

I thought to define a myFunc but don't know if a static counter can retain its state during function calls or use a global counter. but being new to javascript I wanted to ask first. How can this be used in an example, say to count down from 10 to 0? Thanks.

4
  • where does the countdown happen? Commented Apr 10, 2016 at 20:14
  • function repeat(myFunc, cnt) { cnt = cnt || 0; if (myFunc !== undefined && cnt !== 10) { return repeat(myFunc, ++cnt); } } Commented Apr 10, 2016 at 20:17
  • i think your browser is stuck while doing this the function is never stopped Commented Apr 10, 2016 at 20:18
  • 2
    The role of myFunc is irrelavant in the code you have given, either it is undefined from the start, and the function doesn't get called recursively, or it is not, and it runs indefinitely. I don't think Crockford would have presented this code. Maybe you forgot brackets somewhere? I mean: myFunc has to be called at some point... Commented Apr 10, 2016 at 20:20

4 Answers 4

3

You need to call the function myFunc somewhere -- and evaluate the result for further call of repeat.

function repeat(myFunc) {
    if (myFunc()) {
        repeat(myFunc);
    }
}

var count = 10;

repeat(function () {
    document.write(count + '<br>');
    count--;
    return count >= 0;
});
 

Sign up to request clarification or add additional context in comments.

Comments

2

Here is a version that keeps state without global variable:

function repeat(myFunc, arg) {
    if ((arg = myFunc(arg)) !== undefined) {
        repeat(myFunc, arg);
    }
}

repeat(function (count) {
    document.write(count + ',');
    count--;
    if (count >= 0) return count;
}, 10);
 

Comments

2

How can this be used in an example, say to count down from 10 to 0?

Pass a Number to repeat, call repeat with decremented number as parameter until variable parameter is equal to 0

function repeat(n) {
  console.log(n)
  if (n) {
    return repeat(--n);
  }
}

repeat(10)

4 Comments

The name myFunc is a bit misleading in this case :)
@trincot Yes. Tried to change original js at OP at little as possible. myFunc could be any varible; updated variable name to n
@guest271314 your solution does not print zero at the end. not withstanding this, the reason for the question is to understand how to call a function and the number count down was 'just a use case. Poor one I must add.
@FredJ. "your solution does not print zero at the end." ? Logs 0 at console here. Yes, a function could be called to decrement number passed as parameter, though not necessary to achieve expected results. - operator provides same results as calling a function to do the same.
0

Not sure if I understand what approach you want, but you can use this to count down recursively

function repeat(myFunc, times) {
  if(times > 0 && typeof myFunc == 'function') {
    myFunc(times);
    repeat(myFunc, times-1);
  }
}
repeat(alert, 10);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.