Skip to content

Instantly share code, notes, and snippets.

@SergioR82
Last active November 17, 2018 22:25
Show Gist options
  • Save SergioR82/805a814316fd9f6f77a8fb501089e1be to your computer and use it in GitHub Desktop.
Save SergioR82/805a814316fd9f6f77a8fb501089e1be to your computer and use it in GitHub Desktop.
PowExample implementation using Tail Call Optimization
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