Mode entretien. Minuteur de 10 min, sans indices ni autocomplétion
Words that contain a part
MoyenStrings10 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.
Exemples
filterWords(['ant', 'tiger', 'elephant'], 'ant')Renvoie [ 'ant', 'elephant' ]
filterWords(['Antelope', 'cat', 'PLANT'], 'ant')Renvoie [ 'Antelope', 'PLANT' ]
filterWords(['dog', 'cat'], 'ant')Renvoie []
Posé chez Nvidia · Source
Chargement de l'éditeur...
- Entrée
filterWords([ 'ant', 'tiger', 'elephant', 'pants' ], 'ant')Attendu[ 'ant', 'elephant', 'pants' ] - Entrée
filterWords([ 'dog', 'cat' ], 'ant')Attendu[]
Les tests cachés se lancent à la soumission