You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
765 B
39 lines
765 B
// FINDS FREQUENCIES OF WORDS IN A SENTENCE
|
|
wordFreq = function(string) {
|
|
|
|
// split the sentence into individual words
|
|
var wordArray = string.split(' ');
|
|
|
|
// set up default values for each unique word in an object
|
|
var words = {}
|
|
for (var i=0; i < wordArray.length; i++) {
|
|
wordArray[i] = wordArray[i].toLowerCase();
|
|
words[wordArray[i]] = 0;
|
|
}
|
|
|
|
// console.log(words);
|
|
|
|
// increment values for each word by frequency
|
|
for (var j=0; j < wordArray.length; j++) {
|
|
words[wordArray[j]] += 1;
|
|
}
|
|
|
|
return bigFreq(words);
|
|
}
|
|
|
|
// RETURNS HIGHEST VALUE KEY-VALUE PAIR
|
|
bigFreq = function(obj) {
|
|
var value = -Infinity;
|
|
var word = null;
|
|
|
|
for (var key in obj) {
|
|
if (obj[key] > value) {
|
|
value = obj[key]
|
|
word = key;
|
|
}
|
|
}
|
|
|
|
return { [word] : value }
|
|
}
|
|
|