Code Lab

Modo entrevista. Temporizador de 15 min, sin pistas y sin autocompletado

Palindrome words in a sentence

DifícilStrings15 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.

Ejemplos

findPalindromeWords('Mom and Dad saw a racecar')

Devuelve { words: [ 'mom', 'dad', 'a', 'racecar' ], count: 4 }

findPalindromeWords('Wow! Noon, level.')

Devuelve { words: [ 'wow', 'noon', 'level' ], count: 3 }

Preguntada en Goomo · Fuente

Cargando el editor…
  1. EntradafindPalindromeWords('Mom and Dad saw a racecar')
    Esperado{ words: [ 'mom', 'dad', 'a', 'racecar' ], count: 4 }
  2. EntradafindPalindromeWords('This is a test')
    Esperado{ words: [ 'a' ], count: 1 }

Las pruebas ocultas se ejecutan al enviar