Palindrome words in a sentence
HardStrings15 min
Write a function findPalindromeWords(sentence) that finds all palindrome words in a sentence and returns an object { words, count }.
- A word is a run of Latin letters
a-zandA-Z. Every other character (spaces, punctuation, digits, letters with accents such asé) separates words:'Wow!'is the wordwow, and'101'is not a word. - The check is case insensitive:
'Mom'is a palindrome. wordsis an array of the palindrome words in lower case, in the order they appear in the sentence. A word that appears twice is in the array twice.- A word of one letter is a palindrome.
countis the number of items inwords.- No palindrome words (or an empty sentence): return
{ words: [], count: 0 }.
A regular expression such as /[a-z]+/gi finds the words.
Examples
findPalindromeWords('Mom and Dad saw a racecar')Returns { words: [ 'mom', 'dad', 'a', 'racecar' ], count: 4 }
findPalindromeWords('Wow! Noon, level.')Returns { words: [ 'wow', 'noon', 'level' ], count: 3 }
Asked at Goomo · Source
Loading the editor...
- Input
findPalindromeWords('Mom and Dad saw a racecar')Expected{ words: [ 'mom', 'dad', 'a', 'racecar' ], count: 4 } - Input
findPalindromeWords('This is a test')Expected{ words: [ 'a' ], count: 1 }
Hidden tests run on Submit