Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript and the DOM (Retiring) Responding to User Interaction Delaying Execution with setTimeout()

Tim Morgan
Tim Morgan
9,392 Points

Recursion

As in other programming languages, I just want to be clear that these are examples of recursive logic?

5 Answers

Nope, a recursive function calls itself. In the video it is described how you can pass a function (somthing) => {console.log(something);} inside of another function: window.setTimeOut() .

A recursive function is something like:

let recursiveFunction = (n) => {
    //stop condition
    if(n === 1 || n === 2){
        return 1;
    }

    //call
    return recursiveFunction (n - 2) + recursiveFunction (n - 1);
}

for(let i = 1; i < 20; i ++){
    document.write("<p>fib(" + i +"): " + recursiveFunction(i) + "</p>");
}

This writes out all the fibonacci numbers from 1 to 20.

Lucas Sacramento
Lucas Sacramento
912 Points

Recursion is a technique for iterating over an operation by having a function call itself repeatedly until it arrives at a result. So, for this example, recursion is not being used. It's a self executing function which receives something = "Greetings, everyone!" as parameter.

function recurse(depth)
{
 try
 {
  return recurse(depth + 1);
 }
 catch(ex)
 {
  return depth;
 }
}

var maxRecursion = recurse(1);
document.write("Recursion depth on this system is " + maxRecursion);

Have fun :D

Tim Morgan
Tim Morgan
9,392 Points

Ahhh.. sweet clarity. Thanks so much, I guess I'm confusing the 'calling within' technique with the iteration calling itself repeatedly, although the concepts seem may seem somewhat connnected, they are fundamentally different! Thanks

Ahhh man sorry i missed that "Watch Vdieo" Button on the top^^

I thought you wanted just an example of recursion. I should not answer questions when I am out of time

Tim Morgan
Tim Morgan
9,392 Points

Naw, it's still good - I am trying to get past being stuck in the concepts, and just apply the programming logic. It's 'easy' but it's complicated to sort through sometimes, anyway I tried your example, and it helps a lot!