Skip to content

Instantly share code, notes, and snippets.

@valeriecodes
Created February 27, 2018 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valeriecodes/f0e0f0b205be1365454e79f7ef614c69 to your computer and use it in GitHub Desktop.
Save valeriecodes/f0e0f0b205be1365454e79f7ef614c69 to your computer and use it in GitHub Desktop.
All Tests Pass
/**
* Week 6: Anagram check (or, hacker nag cam?)
*
* Given a function 'anagramCheck(a, b)', return true if the two strings are anagrams of each other, false otherwise.
*/
function anagramCheck(a, b) {
let aArray = a.toLowerCase().replace(/\s/g, '').split(''),
bArray = b.toLowerCase().replace(/\s/g, '').split('');
aArray.sort();
bArray.sort();
if (aArray.length !== bArray.length){
return false;
}
for (let i = 0; i < aArray.length; i++) {
if (aArray[i] !== bArray[i]) {
return false;
}
}
return true;
}
assert(anagramCheck("foo", "foo") === true, "Matching strings are anagrams.");
assert(anagramCheck("foo", "foobarbaz") === false, "Mismatched strings cannot be anagrams.");
assert(anagramCheck("acres", "races"), "acres is an anagram of races");
assert(anagramCheck("cat", "dog") === false, "cat is not an anagram of dog");
assert(anagramCheck("CAT", "cat"), "case sensitivity check: CAT is an anagram to cat");
assert(anagramCheck("The eyes", "They see"), "multi-word anagram");
assert(anagramCheck("William Shakespeare", "I am a weakish speller"), "multi-word anagram, pt 2");
assert(anagramCheck("Tom Marvolo Riddle", "I am Lord Voldemort"), "multi-word anagram, pt 3");
assert(anagramCheck("anagram check", "hacker nag cam"), "punny test");
console.log("All clear 🎉");
// Assertion library
/**
* Basic assert.
*
* Example: assert(foo == true, "Foo equal true");
*
* @param assertion
* @param message
*/
function assert(assertion, message) {
if (!assertion) {
console.trace();
throw (!!message) ? `Assertion fail: ${message}.` : `Assertion fail.`;
} else {
console.log((!!message) ? `Pass: ${message}` : "Assertion passed.");
}
}
/**
* Helper function for deep object equal.
*
* Example: assertEqual({foo: "bar"}, {foo: "bar"}, "Objects equal.");
*
* @param first
* @param second
* @param message
*/
function assertObjectEqual(first, second, message) {
if (!first || !second) {
throw (!!message) ? `Assertion fail: ${message}.` : `Assertion fail.`;
}
let firstKeys = Object.keys(first),
secondKeys = Object.keys(second);
if (firstKeys.length !== secondKeys.length) {
throw (!!message) ? `Assertion fail: ${message}.` : `Assertion fail.`;
}
firstKeys.forEach((key) => {
if (typeof first[key] === "object") {
assertObjectEqual(first[key], second[key], message);
} else if (first[key] !== second[key]) {
throw (!!message) ? `Assertion fail: ${message}.` : `Assertion fail.`;
}
});
console.log((!!message) ? `Pass: ${message}` : "Assertion passed.");
}
/**
* Assert equal.
*
* Example: assertEqual(true, true, "True equals true.");
*
* @param first
* @param second
* @param message
*/
function assertEqual(first, second, message) {
if (typeof first === "object") {
assertObjectEqual(first, second, message);
} else {
assert(first === second, message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment