So, the input "Able was I ere I saw Elba" should count as a palindrome.
Approach:
Let's assume the signature of our function is palindrome(s) where s is the input
string. In the body of your function, check if the length of s is less than 2. If so, just
return true because a string containing one letter is the same backwards and
forwards. If length of s is greater than 2, take the first and last letters off of the string
and determine if they are equal. If not, return false. But if they are equal, recursively
call palindrome on the sub-string of s that excludes both the first and last
character. That means the length of the new string should be two characters less than
that of the original string.
Writing any explicit loop in your code results a 0 for this question. Remember to provide
pre- and post- conditions.
Now copy/paste the following trace table in your word file and trace your function
palindrome(s) for when s is initially "racecar". As a guideline, we have populated
the tables withthe values for the first call and the corresponding returned values for
each of those calls. As ever, the first table is populated top down (i.e. time elapses
from row x torow x+1) whereas the returned value table is populated bottom-up
(i.e. time elapses from row x to row x-1). You may want to use debugging features
console.log to observe thesevalues as your program runs.
call#
s
value returned to this
call
1
"racecar"
TRUE
2
"aceca"
TRUE