Skip to content

Instantly share code, notes, and snippets.

Created July 31, 2016 19:43
Show Gist options
  • Save anonymous/dc8cd639591d100864aa5260f766fdc7 to your computer and use it in GitHub Desktop.
Save anonymous/dc8cd639591d100864aa5260f766fdc7 to your computer and use it in GitHub Desktop.
https://repl.it/C1Tp/89 created by sethopia
/*
Anagramatic
Write a function that takes in two strings and determines if they are anagrams of each other. Something is an anagram of something else if it is made up of the same characters.
For example ekez is an anagram of zeke. zkee is also an anagram of zeke.
Your function should return true or false.
anagram('zeke', 'hello world');
// -> false
anagram('nick', 'ncki');
// -> true
*/
function anagram(word1, word2) {
if (word1.length !== word2.length) return false;
for (var i = 0; i < word1.length; i++) {
if (word2.indexOf(word1.charAt(i)) === -1) return false;
}
return true;
}
console.log(anagram('zeke', 'hello world'));
console.log(anagram('nick', 'ncki'));
console.log(anagram('qo', 'oq'));
Native Browser JavaScript
>>> false
true
true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment