Created
March 14, 2016 02:52
-
-
Save skbailey/8431577e2941387ac1c8 to your computer and use it in GitHub Desktop.
Examples of Recursion
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
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