Files
JavaScript/String/CheckPalindrome.js
Lokesh Patil 4bc2b28b7e #559 Improve Palindrome check
* #559

* #559

* #559

* #559

Co-authored-by: LOkesh <lpatil@rewardhealth.com>
2021-02-17 01:48:00 +05:30

18 lines
603 B
JavaScript

// Palindrome check is case sensitive; i.e. Aba is not a palindrome
// input is a string
const checkPalindrome = (str) => {
// check that input is a string
if (typeof str !== 'string') {
return 'Not a string'
}
if (str.length === 0) {
return 'Empty string'
}
// Reverse only works with array, thus conevert the string to array, reverse it and convert back to string
// 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'
}
export { checkPalindrome }