That’s my example. String are given. Implement a function – detectPalindrom
, that can detect palindrome string.
- Given argument not an string – return ‘Passed argument is not a string’.
- Given string is empty – return ‘String is empty’.
- Given string palindrome – return ‘This string is palindrome!’.
- Given string is not a palindrome – return ‘This string is not a palindrome!’
I wrote a solution, but it works incorrectly:
const detectPalindrome = (str) => { const palindr = str.split('').reverse().join('') if(str === '') { return 'String is empty' } if (str === palindr) { return 'This string is palindrome!' } if (str !== palindr) { return 'This string is not a palindrome!' } }
Advertisement
Answer
Just can just put a check before creating the palindr
string.
const detectPalindrome = (str) => { if (typeof str !== "string") { return 'Passed argument is not a string' } const palindr = str.split('').reverse().join(''); if (str === '') { return 'String is empty'; } if (str === palindr) { return 'This string is palindrome!'; } if (str !== palindr) { return 'This string is not a palindrome!'; } }; detectPalindrome("154");