Skip to content

Instantly share code, notes, and snippets.

@sowasred2012
Last active August 29, 2015 13:57
Show Gist options
  • Save sowasred2012/9645682 to your computer and use it in GitHub Desktop.
Save sowasred2012/9645682 to your computer and use it in GitHub Desktop.
Made Code Dojo 2: Pangrams
SUCCESS
## http://en.wikipedia.org/wiki/Pangram
Pangrams (also known as holoalphabetic sentences) are sentences in which every letter of the alphabet is featured at least once.
Write a script that will take a string and return true or false depending on whether that string is a pangram, as well a tally count for each letter of the alphabet.
Pangram examples:
Crazy Fredrick bought many very exquisite opal jewels
Five or six big jet planes zoomed quickly by the tower
Five wine experts jokingly quizzed sample Chablis
How razorback jumping frogs can level six piqued gymnasts
The exodus of jazzy pigeons is craved by squeamish walkers
The quick brown fox jumped over the lazy dog
We promptly judged antique ivory buckles for the next prize
function getAlphabet() {
return 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
}
function hasLetter(str, letter) {
return str.indexOf(letter) === -1
}
function isPanagram(str) {
var alphabet = getAlphabet(),
strUpper = str.toUpperCase(),
i,
letter;
for (i = 0; i < alphabet.length; i++) {
letter = alphabet[i];
if (hasLetter(strUpper, letter)) {
return false;
}
}
return true;
}
function inspectPanagram(str) {
var alphabet = getAlphabet(),
i,
instances = {},
key,
letter,
strUpper = str.toUpperCase();
for (i = 0; i < alphabet.length; i++) {
letter = alphabet[i];
if (instances[letter] === undefined) {
instances[letter] = 0;
}
strUpper.split('').forEach(function (strLetter) {
if (strLetter == letter) {
instances[letter]++;
}
});
}
for (key in instances) {
instances[key.toLowerCase()] = instances[key];
};
instances.count = function (letter) {
return instances[letter];
};
return instances;
}
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