'Why is this anagram function not correct?

Just got back my test for my Javascript class and I missed a question that I felt quite certain about.

We were supposed to make a function that returned true if two strings were anagrams, and false otherwise. I cant find an instance where this wouldn't work. I would appreciate any help!

This is what I had:

function isAnagram(str1, str2){
    if(str1.length !== str2.length){
        return false;
    }
    else{
        for(var i = 0; i < str1.length; i++){
            if(!str2.includes(str1[i])){
                return false;
            }
         }
         return true;
     }
}


Solution 1:[1]

Alternative solution.

const isAnagram = (s1, s2) => {
  return lowerify(s1) == lowerify(s2);
}

const lowerify = str => str.toLowerCase().split('').sort().join('');

console.log(isAnagram("Arrigo Boito", "Tobia Gorrio"));
console.log(isAnagram("abc", "CBa"));
console.log(isAnagram("deep", "depp"));

Solution 2:[2]

It can't handle the case where you have two words, of equal length, using the same letters, but with different numbers of each letter.

This gives a false positive:

console.log(isAnagram("deep", "depp"))

Solution 3:[3]

Because your function would, for example, fail this test:

isAnagram('abba', 'abbb') === false

Solution 4:[4]

as a test case, your code would return success on this, but violate the rules of being an anagram :

isAnagram("ooaooa", "oooooa");

it just checks if the same amount of letters are present, and if every letter from word a is present in word b, but not the count of the letters in both words.

Solution 5:[5]

Single characters can occur multiple times in your function, for example comparing test and tttt returns true. One way to modify your function is to remove the character from str2 each time one is found in the for loop:

function isAnagram(str1, str2){
  if(str1.length !== str2.length) return false;
  for(var i = 0; i < str1.length; i++){
    var found = str2.indexOf(str1[i]);
    if(found === -1) return false;
    str2 = str2.substr(0,found) + str2.substr(found + 1)
  }
  return true;
}

Solution 6:[6]

Simply compare both the strings character by character and register how many matches you find. The total matches must be >= to the string length (to cater to the cases where the characters might be repeating e.g. boot)

var i, j;
var z = 0;
function checkAnagram(str1, str2) {
    if (str1.length != str2.length) { return `${str1} and ${str2} are not anagram, lengths dont match!`; }
    else {
        for (i = 0; i < str1.length; i++) {
            for (j = 0; j < str2.length; j++) {
                if(str1[i] == str2[j]) {
               // console.log('matches',str1[i],str2[j]);
                z++;
                }
                }
            }
          //  console.log(z);
            if (z>=str1.length)
            return`${str1} and ${str2} are anagram!`;
            else return `${str1} and ${str2} are not anagram!`;
        }
}
console.log(checkAnagram('fired','fried'));
console.log(checkAnagram('logo','ogol'));
console.log(checkAnagram('abcd','abc'));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kind user
Solution 2 Quentin
Solution 3
Solution 4 Timothy Groote
Solution 5
Solution 6