solution for javascript/anagrams

pull/2/head
Raphael 2019-09-13 11:27:31 +02:00 committed by GitHub
parent ff31701d29
commit 68436481af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 0 deletions

View File

@ -0,0 +1,18 @@
// Solution
function anagrams(word, words) {
const sort = (word) => {
return word.split('').sort().join('');
}
return words.filter(w => sort(w) === sort(word));
}
// Tests
describe("Solution", function() {
it("should find anagrams", function() {
Test.assertDeepEquals(anagrams('abba', ['aabb', 'abcd', 'bbaa', 'dada']), ['aabb', 'bbaa']);
Test.assertDeepEquals(anagrams('racer', ['crazer', 'carer', 'racar', 'caers', 'racer']), ['carer', 'racer']);
Test.assertDeepEquals(anagrams('laser', ['lazing', 'lazy', 'lacer']), []);
Test.assertDeepEquals(anagrams('alert', ['alter', ,'alper', 'later']), ['alter', 'later']);
});
});