Instantly share code, notes, and snippets.

Embed
What would you like to do?
Recursion - Raise value to power n
function PowExample(val, exp){
//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. return (exp === 1)? val will be executed
//when PowExample has been invocated at least once.
//Recursive Call => val * PowExample(val, exp-1)
return (exp === 1)? val : val * PowExample(val, exp-1);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment