Code Lab

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-z and A-Z. Every other character (spaces, punctuation, digits, letters with accents such as é) separates words: 'Wow!' is the word wow, and '101' is not a word.
  • The check is case insensitive: 'Mom' is a palindrome.
  • words is 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.
  • count is the number of items in words.
  • 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...
  1. InputfindPalindromeWords('Mom and Dad saw a racecar')
    Expected{ words: [ 'mom', 'dad', 'a', 'racecar' ], count: 4 }
  2. InputfindPalindromeWords('This is a test')
    Expected{ words: [ 'a' ], count: 1 }

Hidden tests run on Submit.