Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

193
Visualizações
Determine which strings contain the following characters in a list

I am trying to write javascript code that allows me to extract which words (given as a list) contain either 1 or more characters in a given list of words.

function filterWords(words, letters) {
    var myArrayOfLetters = Array.from(letters)

    var output;
    for(var i = 0; i<myArrayOfLetters.length; ++i)
    {
        output = words.filter(word => word.includes(myArrayOfLetters[i]))
    }

    return output;
}

This is the code I have so far but it is not working.

This is an example of the input and the output required:

words = ['the','dog','got','a','bone']
letters = 'ae'

output = ['the','a','bone']

As you can see the output contains all the words that have either 'a' or 'e' in them or both.

How would I go about solving this issue?

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You're on the right track, but you have your loops backward. You want to use the filter as the outer loop, and the loop over the array of letters as the inner loop, since you want to know if a word has any of those letters in it. The inner loop can be a call to the some method rather than an explicit loop:

function filterWords(words, letters) {
    const myArrayOfLetters = Array.from(letters);
    const output = words.filter((word) =>
         myArrayOfLetters.some((letter) => word.includes(letter))
    );
    return output;
}

some returns true if the callback ever returns true (or any truthy value), or false if the callback never does.

Live Example:

function filterWords(words, letters) {
    const myArrayOfLetters = Array.from(letters);
    const output = words.filter((word) => myArrayOfLetters.some((letter) => word.includes(letter)));
    return output;
}
const words = ["the", "dog", "got", "a", "bone"];
const letters = "ae";
console.log(filterWords(words, letters));

Here's an example with a word that has both a and e in it; note it's only included once:

function filterWords(words, letters) {
    const myArrayOfLetters = Array.from(letters);
    const output = words.filter((word) => myArrayOfLetters.some((letter) => word.includes(letter)));
    return output;
}
const words = ["the", "dog", "got", "a", "really", "nice", "bone"];
const letters = "ae";
console.log(filterWords(words, letters));

about 4 years ago · Santiago Trujillo Relatório

0

You can simply achieve it with the help of Array.filter() along with Array.some() method.

Live Demo :

let words = ['the', 'dog', 'got', 'a', 'bone'];
const letters = 'ae';

const splittedLetters = letters.split('');

const res = words.filter(item => {
    return splittedLetters.some(word => item.includes(word));
});

console.log(res);

about 4 years ago · Santiago Trujillo Relatório

0

let words = ['javascript', 'java', 'ruby', 'python', 'php'];
let filterWords =(words, chars) => {
      return words.filter(word => word.split('').some(char => chars.includes(char)));
}
console.log(filterWords(words, 'pa')); //['JavaScript', 'Java', 'Python', 'PHP']
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda