Common Character Count in Strings JavaScript

Here is the problem:

Given two strings, find the number of common characters between them.

For s1 = "aabcc" and s2 = "adcaa", the output should be 3.

I have written this code :

function commonCharacterCount(s1, s2) {
  var count = 0;
  var str = "";
  for (var i = 0; i < s1.length; i++) {
    if (s2.indexOf(s1[i]) > -1 && str.indexOf(s1[i]) == -1) {
      count++;
      str.concat(s1[i])
    }
  }

  return count;
}

console.log(commonCharacterCount("aabcc", "adcaa"));

It doesn’t give the right answer, I wanna know where I am wrong?

Answer

There are other more efficient answers, but this answer is easier to understand. This loops through the first string, and checks if the second string contains that value. If it does, count increases and that element from s2 is removed to prevent duplicates.

function commonCharacterCount(s1, s2) {
    var count = 0;
    s1 = s1.split('');
    s2 = s2.split('');
    
    s1.forEach(e => {
      if (s2.includes(e)) {
        count++;
        s2.splice(s2.indexOf(e), 1);
      }
    });
        
    return count;
}

console.log(commonCharacterCount("aabcc", "adcaa"));