Last active
November 17, 2018 22:25
-
-
Save SergioR82/805a814316fd9f6f77a8fb501089e1be to your computer and use it in GitHub Desktop.
PowExample implementation using Tail Call Optimization
This file contains 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
function PowTCOExample(val, exp, result = 1){ | |
//Safe case to catch all possible errors | |
if(val == 0 || typeof val != 'number' | |
|| typeof exp != 'number') return 0; | |
//First base case. Avoid recursivity when | |
//power is 0. | |
if(exp === 0) return 1; | |
//Second base case. | |
if(exp === 1) return (val * result); | |
return PowTCOExample(val, exp-1, val * result); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment