Modo entrevista. Temporizador de 10 min, sin pistas y sin autocompletado
Words that contain a part
MediaStrings10 min
Write a function filterWords(words, part) that returns the words from the array words that contain part as a substring. For ['ant', 'tiger', 'elephant'] and 'ant' the answer is ['ant', 'elephant'].
wordsis an array of words (it can be empty).partis a non-empty string of letters.- The search is case insensitive: with
'ant'the words'Antelope'and'PLANT'match too, and'ANT'aspartfinds'plant'. partcan be anywhere in the word: at the start, in the middle or at the end.- Keep the words in their order and with their own spelling. If a word is in the list twice, it is in the answer twice.
- If nothing matches, return an empty array.
- Return a new array. Do not change
words.
includes and filter do most of the work.
Ejemplos
filterWords(['ant', 'tiger', 'elephant'], 'ant')Devuelve [ 'ant', 'elephant' ]
filterWords(['Antelope', 'cat', 'PLANT'], 'ant')Devuelve [ 'Antelope', 'PLANT' ]
filterWords(['dog', 'cat'], 'ant')Devuelve []
Preguntada en Nvidia · Fuente
Cargando el editor…
- Entrada
filterWords([ 'ant', 'tiger', 'elephant', 'pants' ], 'ant')Esperado[ 'ant', 'elephant', 'pants' ] - Entrada
filterWords([ 'dog', 'cat' ], 'ant')Esperado[]
Las pruebas ocultas se ejecutan al enviar