Bummer! You must be logged in to access this page.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

JavaScript Function Explanation

I am (once again) trying to get my head around JavaScript and I am currently working through the MDN guide and in the functions section there is some code I do not understand. Can someone explain to me how it works?

function factorial(n){
  if ((n == 0) || (n == 1))
    return 1;
  else
    return (n * factorial(n - 1));
}

var a, b, c, d, e;
a = factorial(1); // a gets the value 1
b = factorial(2); // b gets the value 2
c = factorial(3); // c gets the value 6
d = factorial(4); // d gets the value 24
e = factorial(5); // e gets the value 120

I don't understand how the function evaluates to the numbers in the comments ... What I see is (with d for example): n=4 so the function returns 4 * (4-1), which is 4 * 3, which equals 12, not 24??

I don't get it.

Thanks!

Duplicate question deleted.

1 Answer

It is calling itself, so when you enter 3, 3 is not 0 or 1, so it does the else. The else takes 3 and multiplies by the function return of 3-1 or 2. When it executes on 2, it again calls itself for 1, which returns 1 and does not call the function again.

So you have

n * (n-1) * (n-1-1) = 6 where n is 3

I want to add that a function called within itself is an example of recursion. So this would be a recursive function.

Hi Ted,

Thanks for your response. I guess that clears it up a little ... but would the function not always just return 1 then? Because n at some point will always be 1 and the conditional states that if it is 1 then the function returns 1?

Apologies if these questions are dumb, I have been trying to 'get' JavaScritp for a while now, but I just can't seem to grasp it at all.

It would not just return 1. It would return this using 3 as an example:

Return (3 * (return 2 * (return 1)))

Looking at my answer from 2 am, it is not that clear.

3 is not 0 or 1, so it does the else and multiplies 3 and the result of the function for 3-1 = 2. When it puts 2 in the code, it is not 0 or 1, so it returns 2 * the result of the function for 2-1=1. To work from inside out, you get 1*2*3=6.

This is not a JavaScript issue. This is a recursive function issue. I think most languages have recursive functions, but I am not sure. Let me know if you still don't understand and I will come up with a different explanation.

Thanks Ted, that clears it up better, trying to get my head around JavaScript, or just programming in general has been a big challenge for me, and I guess there will be other times when I am scratching my head wondering what the hell is going on, but I will stick at it.

Thanks again.

What I do when I don't understand code like this is to think through the code as if I was an example input. Using n=3 is a good example. I go through each line of code replacing n with 3 from the beginning. But instead of reading from top to bottom, I read from the top until there is a jump. I follow the jump because the 3 doesn't care about code not being applied to it. That way I learn the logic of the code. In more complicated examples I will take notes with line numbers if I have them.