Checking whether one string matches another
If your input strings are of length n, your current implementation has a cubic runtime complexity O(n³). This could become an issue for longer strings. 

I recommend to split counting character frequencies and comparing those counts into two separate steps with nearly linear complexity O(n):



// Count characters in 'str':
function frequencies(str) {
  let result = {};
  for (let chr of str) {
    result[chr] = (result[chr] || 0) + 1;
  }
  return result;
}

// Check if 'str1' contains all characters of 'str2':
function scramble(str1, str2) {
  let frequencies1 = frequencies(str1);
  let frequencies2 = frequencies(str2);
  return Object.entries(frequencies2).every(([chr2, frequency2]) =&gt;
    frequencies1[chr2] &gt;= frequency2
  );
}

// Examples:
console.log(scramble("abc", "cba"));
console.log(scramble("aa", "aaaa"));
console.log(scramble("123", "234"));