mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 15:39:42 +08:00
26 lines
533 B
JavaScript
26 lines
533 B
JavaScript
/**
|
|
* @function Palindrome
|
|
* @description Check whether the given string is Palindrome or not.
|
|
* @param {String} str - The input string
|
|
* @return {Boolean}.
|
|
* @see [Palindrome](https://en.wikipedia.org/wiki/Palindrome)
|
|
*/
|
|
|
|
const palindrome = (str) => {
|
|
if (typeof str !== 'string') {
|
|
throw new TypeError('Invalid Input')
|
|
}
|
|
|
|
if (str.length <= 1) {
|
|
return true
|
|
}
|
|
|
|
if (str[0] !== str[str.length - 1]) {
|
|
return false
|
|
} else {
|
|
return palindrome(str.slice(1, str.length - 1))
|
|
}
|
|
}
|
|
|
|
export { palindrome }
|