2025-09-04 07:02:03 +03:00
|
|
|
// Palindrome check is case sensitive; i.e., Aba is not a palindrome
|
2020-06-30 09:34:33 +05:30
|
|
|
// input is a string
|
|
|
|
|
const checkPalindrome = (str) => {
|
|
|
|
|
// check that input is a string
|
|
|
|
|
if (typeof str !== 'string') {
|
|
|
|
|
return 'Not a string'
|
|
|
|
|
}
|
2021-02-17 01:48:00 +05:30
|
|
|
if (str.length === 0) {
|
2020-06-30 09:34:33 +05:30
|
|
|
return 'Empty string'
|
|
|
|
|
}
|
2021-10-05 12:49:23 +05:30
|
|
|
// Reverse only works with array, thus convert the string to array, reverse it and convert back to string
|
2021-02-17 01:48:00 +05:30
|
|
|
// return as palindrome if the reversed string is equal to the input string
|
|
|
|
|
const reversed = [...str].reverse().join('')
|
|
|
|
|
return str === reversed ? 'Palindrome' : 'Not a Palindrome'
|
2020-06-30 09:34:33 +05:30
|
|
|
}
|
|
|
|
|
|
2020-10-04 14:38:48 -03:00
|
|
|
export { checkPalindrome }
|