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

265
Visualizações
How to find anagrams of each word contained by a sentence using JavaScript
E.g.:
'abba' and 'baab' are equal
'abba' and 'bbaa' are equal
'abba' and 'abbba' are NOT equal
'abba' and 'abca' are NOT equal

You have to write a function which finds all the anagrams of each word contained by a sentence from a list with words (IMPORTANT: the words from sentence are separated by space only).

You will be given 2 inputs: a sentence and an array with words. You MUST return an array of all the anagrams or an empty array if there are none.

Note:

  • Your solution will be tested automatically; make sure to provide running code
  • Don't focus on creating any visuals, just JavaScript
  • If your code hangs or takes too long to run, your solution won't be evaluated
  • You're free to create additional functions, but keep solution as the main function
  /* your code here */

    const sort = (word) => word.split('').sort().join('');

    function anagrams(word, words) {
        let token = sort(word);

        return words.filter((w) => sort(w) === token);
    }

    console.log(anagrams('abba', ['aabb', 'abcd', 'bbaa', 'dada']));


}

// test your solution
solution('dvvd  pddp', ['ddvv', 'dvcd', 'vvdd', 'pdpd'])
// ['ddvv', 'vvdd', 'pddp']

solution('laser space', ['lazing', 'lazy', 'lacer'])
// []
solution('We will eat tenderising meat at Rivera with no regally plate because there is none',
  ['administration', 'ingredients', 'admit', 'beat', 'arrive', 'blood', 'door', 'each', 'on', 'economic', 'gallery', 'edge', 'three', 'drop'])
// ['ingredients', 'arrive', 'on', 'gallery', 'three'] ```

How exactly I should test my solutions?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Make a function that sorts the letters of a word (lower cased) and returns that sorted anagram.

Use that function to create all sorted words from the input phrase, and turn it into a set.

Finally, iterate the anagrams array, and for each of those words, see if the sorted version of it is in the set. If so, it should be in the result.

How exactly I should test my solutions?

Use the examples of input/output that are given in the code challenge.

Run your solution function with those inputs, and verify that the returned value is as expected. See how it is done below:

function solution(phrase, anagrams) {
    const sortWord = word => [...word.toLowerCase()].sort().join("");

    let words = new Set(phrase.match(/\S+/g).map(sortWord));
    return anagrams.filter(anagram => words.has(sortWord(anagram)));
}

// tests
const tests = [
    { input: ['dvvd  pddp', ['ddvv', 'dvcd', 'vvdd', 'pdpd']],
      expected: ['ddvv', 'vvdd', 'pdpd'] },
    { input: ['laser space', ['lazing', 'lazy', 'lacer']],
      expected: [] },
    { input: ['We will eat tenderising meat at Rivera with no regally plate because there is none', ['administration', 'ingredients', 'admit', 'beat', 'arrive', 'blood', 'door', 'each', 'on', 'economic', 'gallery', 'edge', 'three', 'drop']],
      expected: ['ingredients', 'arrive', 'on', 'gallery', 'three'] }
];

for (let { input, expected } of tests) {
    let output = solution(...input);
    if (JSON.stringify(output) !== JSON.stringify(expected)) {
        throw "got " + JSON.stringify(output) + ", but expected " + JSON.stringify(expected);
    }
}
console.log("all tests passed");

about 4 years ago · Juan Pablo Isaza 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