I need to write a function that counts the frequencies of each word in the string in the given parameter of a function. I was wondering, what is the best way to do this?
For example, if the string in the parameter was "Mark cow mark cow the" The count for each word would be:
Mark: 1
mark: 1
cow: 2
the: 1
My function:
function calcFrequencies(s){
var a = [], b = [], prev;
var words = s.split(" ");
for ( var i = 0; i < words.length; i++ ) {
}
}
Just to clarify, I am okay with whatever solution you come up with, I just figured it probably could involve splitting and looping through arrays, I just can't seem to figure it out.