Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of I need to write difficult palindrome without wasting too much if your time.
The question is published on by Tutorial Guruji team.
The question is published on by Tutorial Guruji team.
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!' } }
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");
We are here to answer your question about I need to write difficult palindrome - If you find the proper solution, please don't forgot to share this with your team members.