Skip to content

Instantly share code, notes, and snippets.

@lkrych
Created February 22, 2017 16:21
Show Gist options
  • Save lkrych/14d9210cdc677ec86d7653b148388663 to your computer and use it in GitHub Desktop.
Save lkrych/14d9210cdc677ec86d7653b148388663 to your computer and use it in GitHub Desktop.
javascript function that specifies whether two words are anagrams
// Anagrams are two words with exactly the same letters. Order does not matter. Define a method that, given two strings, returns a boolean indicating whether they are anagrams.
function anagram(str1,str2){
if (str1.length != str2.length){ //if the words aren't the same length, they aren't anagrams
return false;
}else{
if(str1.split("").sort().join("") === str2.split("").sort().join("")){ //String comparison, clever? if the sorted characters of the words aren't the same, they aren't anagrams.
return true;
}else{
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment