|
| 1 | +// Palindrome check is case sensitive; i.e. Aba is not a palindrome |
| 2 | + |
| 3 | +const checkPalindrome = str => { |
| 4 | + // check that input is a string |
| 5 | + if (typeof str !== 'string') { |
| 6 | + return 'Not a string' |
| 7 | + } |
| 8 | + if (str.length === 0) { |
| 9 | + return 'Empty string' |
| 10 | + } |
| 11 | + // Reverse only works with array, thus convert the string to array, reverse it and convert back to string |
| 12 | + // return as palindrome if the reversed string is equal to the input string |
| 13 | + const reversed = [...str].reverse().join('') |
| 14 | + return str === reversed ? 'Palindrome' : 'Not a Palindrome' |
| 15 | +} |
| 16 | + |
| 17 | +console.log(checkPalindrome('hello')) |
| 18 | +console.log(checkPalindrome(12)) |
| 19 | +console.log(checkPalindrome(null)) |
| 20 | +console.log(checkPalindrome(undefined)) |
| 21 | +console.log(checkPalindrome('level')) |
0 commit comments