Skip to content

Instantly share code, notes, and snippets.

@skbailey
Created March 14, 2016 02:52
Show Gist options
  • Save skbailey/8431577e2941387ac1c8 to your computer and use it in GitHub Desktop.
Save skbailey/8431577e2941387ac1c8 to your computer and use it in GitHub Desktop.
Examples of Recursion
var factorial = function(num) {
if (num < 2) {
return 1;
}
return num * factorial(num - 1);
};
var isPalindrome = function (expression) {
// A single character string eg 'a' and the empty string "" are palindromes
// since they read the same forwards and backwards
if (expression.length <= 1) {
return true;
}
if (expression.charAt(0) !== expression.charAt(expression.length - 1)) {
return false;
}
var chars = expression.split("");
chars.shift();
chars.pop();
var smallerExpression = chars.join("");
return isPalindrome(smallerExpression);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment