Assignment: map/filter/reduce refresher
Implement a function named palindrome that tells whether a given string
is the same when read left-to-right and right-to-left, ignoring upper/lowercase differences
and any characters outside a to z. For example:
palindrome('ABBA') // true
palindrome('Abba') // true
palindrome('A man, a plan, a canal: Panama!') // trueYou must use the following helper functions (they are provided):
const str2chars = str => [...str]; // convert a string to an array of chars
const isAlpha = char => char.match(/[a-z]/); // char is in the alphabet💡
The question title contains implementation hints.
Solution
const palindrome = str => {
const alphaChars = str2chars(str).map(char => char.toLowerCase()).filter(isAlpha);
return alphaChars.join('') === alphaChars.reverse().join('');
};Your solution will be tested against:
palindrome("Amore, Roma!")
&& palindrome("Madam, I'm Adam!")
&& palindrome("Never odd or even.")
&& !palindrome("Amore")