Created
March 21, 2021 14:03
-
-
Save KakoozaJerry/69f969c7fe2adb647ace2d92fcc15b74 to your computer and use it in GitHub Desktop.
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
// Returns the first character of the string str | |
var firstCharacter = function(str) { | |
return str.slice(0, 1); | |
}; | |
// Returns the last character of a string str | |
var lastCharacter = function(str) { | |
return str.slice(-1); | |
}; | |
// Returns the string that results from removing the first | |
// and last characters from str | |
var middleCharacters = function(str) { | |
return str.slice(1, -1); | |
}; | |
var isPalindrome = function(str) { | |
// base case #1 | |
var result; | |
if(str.length===1|| str.length===0){ | |
return true; | |
} | |
if(firstCharacter(str)!==lastCharacter(str)){ | |
return false; | |
} | |
middleCharacters(str); | |
if(firstCharacter(str)===lastCharacter(str)){ | |
return isPalindrome(middleCharacters(str)); | |
} | |
return true; | |
// base case #2 | |
// recursive case | |
}; | |
var checkPalindrome = function(str) { | |
println("Is this word a palindrome? " + str); | |
println(isPalindrome(str)); | |
}; | |
checkPalindrome("a"); | |
Program.assertEqual(isPalindrome("a"), true); | |
checkPalindrome("motor"); | |
Program.assertEqual(isPalindrome("motor"), false); | |
checkPalindrome("rotor"); | |
Program.assertEqual(isPalindrome("rotor"), true); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment