It might be useful to create an object which contains each character and their count in the string.
e.g.
var str1 = "Foobar";
var str2 = "Foobarbaz";
var str1Count = {
"F": 1,
"o": 2,
"b": 1,
"a": 1,
"r": 1
}
var str2Count = {
"F": 1,
"o": 2,
"b": 2,
"a": 2,
"r": 1,
"z": 1
}
And then compare the counts to see if one string can compose another.
Consideration: I am assuming there could be 'extra' characters in one of the strings. If each string is equal length and we're just trying to see if we can rearrange one into another, the above suggestion about sorting should work just fine.
To create the suggested object, you could do this:
var letterCount = function(str){
var letterCount = {};
str.split('').forEach(function(char){
letterCount[char] = letterCount[char] === undefined ? 1 : letterCount[char] + 1;
});
return letterCount;
}