-
-
Save lsegal/67f4e0e81b271f143f32 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function anagram(word) { | |
var originalWord = wordObject(word); | |
return { | |
matches: function(wordArray) { | |
if (typeof wordArray === 'string'){ | |
wordArray = Array.prototype.slice.call(arguments); | |
} | |
var wordMatches = []; | |
for (var i = 0; i < wordArray.length; i++) { | |
var wordMatcher = wordArray[i]; | |
if (word.length !== wordMatcher.length || word.toLowerCase() === wordMatcher.toLowerCase()) { | |
continue; | |
} | |
var wordToCheck = wordObject(wordMatcher); | |
if (checkWords(originalWord, wordToCheck)) { | |
wordMatches.push(wordMatcher) | |
} | |
} | |
return wordMatches; | |
} | |
}; | |
} | |
function checkWords(word1, word2) { | |
if (Object.keys(word1).length != Object.keys(word2).length) { | |
return false; | |
} | |
for (len in word1) { | |
if (!word1.hasOwnProperty(len)) { | |
continue; | |
} | |
if (word1[len] !== word2[len]) { | |
return false; | |
} | |
} | |
return true; | |
} | |
function wordObject(word) { | |
var charArray = word.split(''); | |
var charCount = {}; | |
for (i = 0; i < charArray.length; i++) { | |
var char = charArray[i].toLowerCase(); | |
if (typeof charCount[char] === "number") { | |
charCount[char]++; | |
} else { | |
charCount[char] = 1; | |
} | |
} | |
return charCount; | |
} | |
module.exports = anagram; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var anagram = require('./anagram'); | |
describe('Anagram', function() { | |
it("no matches",function() { | |
var subject = new anagram("diaper"); | |
var matches = subject.matches([ "hello", "world", "zombies", "pants"]); | |
expect(matches).toEqual([]); | |
}); | |
it("detects simple anagram",function() { | |
var subject = new anagram("ant"); | |
var matches = subject.matches(['tan', 'stand', 'at']); | |
expect(matches).toEqual(['tan']); | |
}); | |
it("does not detect false positives",function() { | |
var subject = new anagram("galea"); | |
var matches = subject.matches(["eagle"]); | |
expect(matches).toEqual([]); | |
}); | |
it("detects multiple anagrams",function() { | |
var subject = new anagram("master"); | |
var matches = subject.matches(['stream', 'pigeon', 'maters']); | |
expect(matches).toEqual(['stream', 'maters']); | |
}); | |
it("does not detect anagram subsets",function() { | |
var subject = new anagram("good"); | |
var matches = subject.matches(['dog', 'goody']); | |
expect(matches).toEqual([]); | |
}); | |
it("detects anagram",function() { | |
var subject = new anagram("listen"); | |
var matches = subject.matches(['enlists', 'google', 'inlets', 'banana']); | |
expect(matches).toEqual(['inlets']); | |
}); | |
it("detects multiple anagrams",function() { | |
var subject = new anagram("allergy"); | |
var matches = subject.matches(['gallery', 'ballerina', 'regally', 'clergy', 'largely', 'leading']); | |
expect(matches).toEqual(['gallery', 'regally', 'largely']); | |
}); | |
it("detects anagrams case-insensitively",function() { | |
var subject = new anagram("Orchestra"); | |
var matches = subject.matches(['cashregister', 'Carthorse', 'radishes']); | |
expect(matches).toEqual(['Carthorse']); | |
}); | |
it("does not detect a word as its own anagram",function() { | |
var subject = new anagram("banana"); | |
var matches = subject.matches(['Banana']); | |
expect(matches).toEqual([]); | |
}); | |
it("matches() accepts string arguments",function() { | |
var subject = new anagram("ant"); | |
var matches = subject.matches("stand", "tan", "at"); | |
expect(matches).toEqual(["tan"]); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment