Front end developer specialising in JavaScript and React. Experienced in all aspects of modern front end development. Passionate about making accessible, secure and performant software.
Good effort, but unfortunately this solution doesn't always work.
E.g. when str1 is aaaaa and str2 is abbbb, it returns true when it should be false.
It's because .includes checks that the letter is anywhere in the string, not caring about how many times. However the number of times the letter appears also needs to match the other string.
Front end developer specialising in JavaScript and React. Experienced in all aspects of modern front end development. Passionate about making accessible, secure and performant software.
That's right. That's what we're testing for: whether they are anagrams of each other. So in the str1 = aaaaa and str2 = abbbb case it should return false because as you say they're not anagrams of each other.
Front end developer specialising in JavaScript and React. Experienced in all aspects of modern front end development. Passionate about making accessible, secure and performant software.
Good effort, but unfortunately this solution doesn't always work.
E.g. when
str1isaaaaaandstr2isabbbb, it returnstruewhen it should be false.It's because
.includeschecks that the letter is anywhere in the string, not caring about how many times. However the number of times the letter appears also needs to match the other string.aaaaaandabbbbis not an anagram in the first place, is it?That's right. That's what we're testing for: whether they are anagrams of each other. So in the
str1 = aaaaaandstr2 = abbbbcase it should returnfalsebecause as you say they're not anagrams of each other.function anagram(str1, str2) {return (
str1.length == str2.length &&
str1.split("").every(c => str2.includes(c)) &&
str2.split("").every(c => str1.includes(c))
);
}
... and its still about 70% faster.
Nice :)
The new method returns
truefor"abb", "aab", I don't think it should.You're right,
everyjust won't work. My method works for a few cases but not all.