-
-
Save lukemorton/9647975 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 getAlphabet() { | |
return 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''); | |
} | |
function hasLetterFn(str) { | |
str = str.toUpperCase(); | |
return function (isPangram, letter) { | |
return isPangram && (str.indexOf(letter) > -1); | |
} | |
} | |
function isPanagram(str) { | |
return getAlphabet().reduce(hasLetterFn(str), true); | |
} | |
function countLettersFn(str) { | |
var strLetters = str.toUpperCase().split(''); | |
return function (instances, letter) { | |
instances[letter] = 0; | |
strLetters.forEach(function (strLetter) { | |
if (strLetter == letter) { | |
instances[letter]++; | |
} | |
}); | |
return instances; | |
} | |
} | |
function inspectPanagram(str) { | |
var instances = getAlphabet().reduce(countLettersFn(str), {}); | |
for (key in instances) { | |
instances[key.toLowerCase()] = instances[key]; | |
}; | |
instances.count = function (letter) { | |
return instances[letter]; | |
}; | |
return instances; | |
} |
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
describe('isPanagram', function () { | |
it('should return true for "Crazy Fredrick bought many very exquisite opal jewels"', function () { | |
expect(isPanagram("Crazy Fredrick bought many very exquisite opal jewels")).toBe(true); | |
}); | |
it('should return false for "A sentence that probably does not have every character"', function () { | |
expect(isPanagram("A sentence that probably does not have every character")).toBe(false); | |
}); | |
}); | |
describe('inspectPangram', function () { | |
it('should return a hash containing A=2 for "Arse Face"', function () { | |
expect(inspectPanagram("Arse Face").a).toBe(2); | |
}); | |
it('should return a hash containing B=1 for "Bum Face"', function () { | |
console.log(inspectPanagram("Bum Face")); | |
expect(inspectPanagram("Bum Face").b).toBe(1); | |
}); | |
it('should return a hash containing b=3 for "Bumblebee Face"', function () { | |
var inspection = inspectPanagram("Bumblebee Face"); | |
expect(inspection.b).toBe(3); | |
expect(inspection.B).toBe(3); | |
}); | |
describe('inspect panagram hash', function () { | |
it('should return b=3 via count method', function () { | |
var inspection = inspectPanagram("Bumblebee Face"); | |
expect(inspection.count('b')).toBe(3); | |
expect(inspection.count('B')).toBe(3); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment