Last active
October 25, 2021 21:33
-
-
Save marcuszierke/efb44599be507d11776aef8938f93ad9 to your computer and use it in GitHub Desktop.
Tail Recursion - Collartz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const simpleFac = (n) => { | |
if (n === 1) return n | |
return n * simpleFac(n - 1) | |
} | |
const TRFac = (n, a) => { | |
if (n === 0) return a | |
return TRFac(n - 1, a * n) | |
} | |
// hier wird der Stack 5 mal gecallt => was Sinn macht | |
console.log(simpleFac(5)) | |
// hier wird der Stack nur ein einziges Mal gecallt | |
// => was es ja auch tun soll - TR - aber sich mir nicht erschliesst | |
console.log(TRFac(5,1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment