'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?



Solution 1:[1]

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");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1