Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

isPalindrome(w) { if (w is empty string or w is of length 1) return true; else i

ID: 441943 • Letter: I

Question

isPalindrome(w) { if (w is empty string or w is of length 1) return true; else if (w

Explanation / Answer

Hi. This recursive function works by comparing a string from the outside characters (or outmost edges and working in. By the definition of a palindrome the first and last characters have to match thus thats what we do here .... 1 --> We enter recursive function isPalindrome(w) with a word denoted (w) passed in. 2 --> 1st check to see if the word passed contains only one character or is simply an empty string. On these conditions if either is true we return true and we are done because by definition a one character or blank character string is indeed a palindrome. If the word passed in contains more than one character then we have more work to do 3 --> OK we have a word with more than 1 character .... 2 or more so lets compare the first and last character now ... do they match ? if so we still have a possible palindrome so what we do is chop off the first and last letters (characters) from the word and we recursively pass the new shorter word void of the first and last letters back into our function ..... and we repeat the exact same checks as above. If the word passes each time here it was indeed a palindrome and will be deemed one from the first case where it contains only one or no characters. else return false the word failed above so if we hit this line it wasn't a palindrome. Heres a sample word Ill pass into this function ---> isPalindrome(level) ok its bigger than one or no characters so we proceed .... do first and last characters match .... yes they do .... chop chop remove the first and last letters from the word and pass the new shortened version back into the function isPalindrome (eve) .... is it blank or larger than one ? yes .... do first and last match ? yes e and e ... chop chop pass newly shortened word back into function isPalindrome(v) .... is it larger than one character or blank ????? yes it only one character ... thus return true this is a valid case .... otherwise at very end we return false ... Hope this is helpful :-)