Created
February 22, 2017 16:21
-
-
Save lkrych/14d9210cdc677ec86d7653b148388663 to your computer and use it in GitHub Desktop.
javascript function that specifies whether two words are anagrams
This file contains hidden or 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
// 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