Hi this is probably an easy question. So I want to make a basic anagram function in Javascript.
The following snippet does not work
anagrams = (phraseOne, phraseTwo) => {
if (phraseOne.split("").sort() === phraseTwo.split("").sort()) {
return true
} else {
return false
}
}
However this does work
anagrams = (phraseOne, phraseTwo) => {
if (phraseOne.split("").sort().join("") === phraseTwo.split("").sort().join("")) {
return true
} else {
return false
}
}
Why? The arrays are identical before you join("") them