Skip to content

Instantly share code, notes, and snippets.

@madhuravius
Created September 4, 2016 23:18
Show Gist options
  • Save madhuravius/54afc5674fc0dc9d1c40268bfbeee699 to your computer and use it in GitHub Desktop.
Save madhuravius/54afc5674fc0dc9d1c40268bfbeee699 to your computer and use it in GitHub Desktop.
victor_exercise_sentence
var sentence_object = {
// also has properties that are passed in:
// sentence (sentence content)
// count (number of words you want returned and position)
count_words: function (){
var sentence = this.sentence;
// split sentence to words
var words = sentence.split(' ');
// create object of words and counts
var words_counts = {};
// for in --> iterate over object properties
for (var word in words)
{
var word_key = words[word];
if (!words_counts.hasOwnProperty(word_key)) {
words_counts[word_key] = 1;
}
else {
words_counts[word_key] ++;
}
}
console.log('count of words in sentence')
console.log(words_counts);
// reverses keys and values to generate counts as key
// with values in an array
var return_counts = {};
for (var word_group in words_counts)
{
var count_key = words_counts[word_group];
// check if count in object list
if (!return_counts.hasOwnProperty(count_key))
{
return_counts[count_key] = [word_group];
}
else if (word_group.indexOf(return_counts[count_key]) < 0)
{
return_counts[count_key].push(word_group);
}
}
for (var return_counts_element in return_counts) {
// alphabetize each internal count
return_counts[return_counts_element].sort();
}
console.log('printing cleaned returned object of sentence');
console.log(return_counts);
return return_counts;
},
return_cleaned_counts: function () {
/*
returns the actual cleaned counts and words as required:
ex: [{'butter': 2}, {'a': 1}, {'betty': 1}]
*/
var cleaned_sentence = this.cleaned_sentence;
var count = this.count;
var return_obj = [], location = Object.keys(cleaned_sentence).length - 1;
while (location >= 0 && count > 0){
//console.log(cleaned_sentence[Object.keys(cleaned_sentence)[location]]);
var internal_counter = Object.keys(cleaned_sentence)[location];
for (var element in cleaned_sentence[internal_counter])
{
var temp_obj = {};
if (location >= 0 && count > 0)
{
temp_obj[cleaned_sentence[internal_counter][element]] = parseInt(internal_counter);
return_obj.push(temp_obj)
count --;
}
}
location --;
}
// printing final result
console.log('printing final result');
console.log(return_obj);
return return_obj;
}
};
sentence_object.sentence = "betty bought a bit of butter but the butter was bitter",
sentence_object.count = 3;
sentence_object.cleaned_sentence = sentence_object.count_words();
// why isn't this returning equal
console.log(sentence_object.return_cleaned_counts() == [{butter: 2}, {a: 1}, {betty: 1}]);
/*
var returnObj = [{'butter': 2}, {'a': 1}, {'betty': 1}];
// {1: [a, betty, bitter, etc.]}
console.log(returnObj == count_words(sentence,count));
var returnObj = [{'butter': 2}, {'a': 1}, {'betty': 1}];
self.assertEqual(count_words(sentence, count), returnObj)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment