Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about "Return Everything"? #5

Closed
XiaofengXie16 opened this issue Apr 23, 2018 · 2 comments
Closed

Question about "Return Everything"? #5

XiaofengXie16 opened this issue Apr 23, 2018 · 2 comments

Comments

@XiaofengXie16
Copy link

In the Return everything section, you put an example of calculating factorial

const fact = n => n === 0
    ? 1
    : n * fact(n - 1)

console.log('Fact of 5: ', fact(5))

With this approach, we will potentially get a stack overflow error , if the number gets large because of recursion and I would love to know what's your thought on this. Other than that, I learned a lot from your repo and thank you for putting these things together.

@cuchi
Copy link

cuchi commented Apr 23, 2018

In natively functional and some other languages, recursive calls are automatically optimized by the compiler, so we don't need to worry about the stack size. In order to be optimized, your recursion call should follow some rules to match the compiler criteria, like a tail-call, for example.

JavaScript does not support it in all platforms yet, maybe that's because the language a big dynamic mess.

@XiaofengXie16
Copy link
Author

XiaofengXie16 commented Apr 23, 2018

@cuchi Thanks for the explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants